Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP image resize function doesn't work properly

I want to resize an image PNG with transparence plz help. Here is the code :

function createThumb($upfile, $dstfile, $max_width, $max_height){
   $size = getimagesize($upfile);
   $width = $size[0];
   $height = $size[1];
   $x_ratio = $max_width / $width;
   $y_ratio = $max_height / $height;
   if( ($width <= $max_width) && ($height <= $max_height)) {
           $tn_width = $width;
           $tn_height = $height;
   } elseif (($x_ratio * $height) < $max_height) {
           $tn_height = ceil($x_ratio * $height);
           $tn_width = $max_width;
   } else {
           $tn_width = ceil($y_ratio * $width);
           $tn_height = $max_height;
   }
   if($size['mime'] == "image/jpeg"){
           $src = ImageCreateFromJpeg($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imageinterlace( $dst, true);
           ImageJpeg($dst, $dstfile, 100);
   } else if ($size['mime'] == "image/png"){
        $src = ImageCreateFrompng($upfile);
        $dst = ImageCreateTrueColor($tn_width, $tn_height);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

      // integer representation of the color black (rgb: 0,0,0)
     $background = imagecolorallocate($dst, 255, 255, 0);
     // removing the black from the placeholder
     imagecolortransparent($dst, $background);

     // turning off alpha blending (to ensure alpha channel information
     // is preserved, rather than removed (blending with the rest of the
     // image in the form of black))
     imagealphablending($dst, false);

     // turning on alpha channel information saving (to ensure the full range
     // of transparency is preserved)
     imagesavealpha($dst, true);


              Imagepng($dst, $dstfile);

      } else {

           $src = ImageCreateFromGif($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imagegif($dst, $dstfile);
   }
}

The image source :

enter image description here

The current result (that I don't want) is :

enter image description here

How can I resize the image and maintain the transparency of the background color? Need help, please. Thanks.

like image 223
Rjaibi Mejdi Avatar asked Sep 27 '11 15:09

Rjaibi Mejdi


People also ask

How to resize a image in PHP?

Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.

How do I resize a PNG in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


1 Answers

imagecopyresampled is in the wrong place. This should be called after you have set the background colour:

$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);

// use imagecolorallocatealpha to set BG as transparent:
$background = imagecolorallocatealpha($dst, 255, 255, 255, 127);

// turning off alpha blending as it's not needed
imagealphablending($dst, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);

// Do this here!
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);

Imagepng($dst, $dstfile);
like image 176
daiscog Avatar answered Oct 10 '22 00:10

daiscog