Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // Recadrage éventuel en cas de dimension inf. à 80px
- if((!is_null($resResized) && (($iNewWidth < 80) || ($iNewHeight < 80)))
- || (($aImgDetails[0] < 80) || ($aImgDetails[1] < 80))) {
- // Création de la source et de la ressource destination
- if(isset($resResized)) $resImg = $resResized;
- else {
- switch($aImgDetails[2]) {
- case IMAGETYPE_GIF : $resImg = @imagecreatefromgif($_FILES['imagefile']['tmp_name']);
- break;
- case IMAGETYPE_JPEG : $resImg = @imagecreatefromjpeg($_FILES['imagefile']['tmp_name']);
- break;
- case IMAGETYPE_PNG : $resImg = @imagecreatefrompng($_FILES['imagefile']['tmp_name']);
- break;
- }
- $iNewWidth = $aImgDetails[0];
- $iNewHeight = $aImgDetails[1];
- }
- if(!$resReframed = @imagecreatetruecolor(80, 80)) my_exit('Erreur GD2 : imagecreatetruecolor()');
- // On tente de deviner la transparence d'origine, si elle existe
- $isAlpha = FALSE;
- switch($aImgDetails[2]) {
- case IMAGETYPE_GIF : $iTransparent = imagecolortransparent($resImg);
- break;
- case IMAGETYPE_JPEG : $iTransparent = FALSE;
- break;
- case IMAGETYPE_PNG :
- $isAlpha = ord(file_get_contents($_FILES['imagefile']['tmp_name'], FALSE, NULL, 25, 1)) & 4;
- if(!$isAlpha) { // Pas d'alpha, on tente de chopper la couleur de transparence
- $iTransparent = imagecolortransparent($resImg);
- }
- break;
- }
- // On prépare une nouvelle couleur de transparence, ou une couleur avec support alpha
- if($aImgDetails[2] == IMAGETYPE_JPEG) {
- $iTransparent = imagecolorallocate($resReframed, 255, 255, 255);
- } elseif($isAlpha) {
- if(!imagealphablending($resReframed, FALSE)) my_exit('Erreur GD2: alpha inactivable sur resReframed');
- $iTransparent = imagecolorallocatealpha($resReframed, 255, 0, 255, 127);
- } elseif(is_null($iTransparent) || !$iTransparent || ($iTransparent == -1)) {
- $iTransparent = imagecolorallocate($resReframed, 255, 0, 255);
- }
- // On remplit l'image recadrée de la nouvelle couleur transparente
- imagefilledrectangle($resReframed, 0, 0, 80, 80, $iTransparent);
- // On calcule les coordonnées du recadrage lui-même
- $iFrameX = Floor((80-$iNewWidth) / 2);
- if($iFrameX < 0) $iFrameX = 0;
- $iFrameY = Floor((80-$iNewHeight) / 2);
- if($iFrameY < 0) $iFrameY = 0;
- // On merge les deux images
- if($isAlpha) {
- // Copie de chaque pixel et allocation de sa valeur alpha (0 = opaque, 127 = 100% transparent)
- for($i = 0; $i < $iNewWidth; $i++) {
- for($j = 0; $j < $iNewHeight; $j++) {
- $aPXAlpha = imagecolorsforindex($resImg, imagecolorat($resImg, $i, $j));
- if($aPXAlpha['alpha'] <= 126) $aPXColor = imagecolorsforindex($resImg, imagecolorat($resImg, $i, $j));
- else $aPXColor = imagecolorsforindex($resReframed, $iTransparent);
- $iColor = imagecolorallocatealpha($resReframed, $aPXColor['red'], $aPXColor['green'], $aPXColor['blue'], $aPXAlpha['alpha']);
- imagesetpixel($resReframed, $i, $j, $iColor);
- }
- }
- imagesavealpha($resReframed, TRUE);
- } else { // l'image cadre n'a pas de couche alpha
- imagecolortransparent($resReframed, $iTransparent);
- if(!@imagecopymerge($resReframed, $resImg, $iFrameX, $iFrameY, 0, 0, $iNewWidth, $iNewHeight, 100)) my_exit('Erreur GD2: impossible de recadrer la ressource ResImg');
- }
- }
- ?>
Add Comment
Please, Sign In to add comment