4

I have this function below to allow me to resize my uploaded image,

function resize_image($image,$width,$height,$scale) 
{
    list($imagewidth, $imageheight, $imageType) = getimagesize($image);
    $imageType = image_type_to_mime_type($imageType);
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

    switch($imageType) 
    {
        case "image/gif":
            $source = imagecreatefromgif($image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            $source = imagecreatefromjpeg($image); 
            break;
        case "image/png":
        case "image/x-png":
            $source = imagecreatefrompng($image); 
            break;
    }

    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

    switch($imageType) 
    {
        case "image/gif":
            imagegif($newImage,$image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            imagejpeg($newImage,$image,90); 
            break;
        case "image/png":
        case "image/x-png":
            imagepng($newImage,$image);  
            break;
    }

    chmod($image, 0777);
    return $image;
}

But this function has a problem which is that it cannot create transparency when I have a png or a gif with a transparent background.

How can I fix this function to allow transparency with php?

hakre
  • 193,403
  • 52
  • 435
  • 836
Run
  • 54,938
  • 169
  • 450
  • 748
  • 2
    possible duplicate of [php resize script doesn't work for transparent gif's](http://stackoverflow.com/questions/724503/php-resize-script-doesnt-work-for-transparent-gifs) – hakre Jul 25 '11 at 17:13
  • 1
    tested it. and the answer from that post and it has a bug too. thanks. – Run Jul 25 '11 at 18:47

4 Answers4

4

Here is my answer,

# This is the resizing/resampling/transparency-preserving magic
    if ( ($imageType == "image/gif") || ($imageType == "image/png") ) 
    {
        $transindex = imagecolortransparent($source);

        if($transindex >= 0) 
        {
            $transcol = imagecolorsforindex($source, $transindex);
            $transindex = imagecolorallocatealpha($newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
            imagefill($newImage, 0, 0, $transindex);
            imagecolortransparent($newImage, $transindex);
        }

        elseif ($imageType == "image/png" || $imageType == "image/x-png") 
        {
            imagealphablending($newImage, false);
            $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
            imagefill($newImage, 0, 0, $color);
            imagesavealpha($newImage, true);
        }
    }

And Work Totally Fine

Run
  • 54,938
  • 169
  • 450
  • 748
3

I found this solution really helpful!

http://www.php.net/manual/ru/function.imagecopyresampled.php#104028

You just have to add this code between imagecreatetruecolor() and imagecopyresampled(). Don't forget to define $type.

$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

// preserve transparency
if($type == "gif" || $type == "png"){
  imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 0, 0, 0, 127));
  imagealphablending($newImage, false);
  imagesavealpha($newImage, true);
}

imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
pmrotule
  • 9,065
  • 4
  • 50
  • 58
1

Looking at an open-sourced smart_resize_image method, it appears they check for imagecolortransparent and, if present, re-apply it to the new image.

May be worth a look to check out that project on github instead of re-inventing the wheel. Also gives you some insight in to how they do it, and is the work of a collaboration of efforts (a lot of people working around known [and unknown] bugs).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • thanks. I tested the smart_resize_image method and it has a bug for sure when you upload a gif transparent. If you scan the code quickly, you can tell easily what it does wrong. – Run Jul 25 '11 at 18:46
0

In order for GD Lib to draw transparency, you have to define a transparent color. You might sample a color somewhere on the graphic and use that.

http://www.php.net/manual/en/function.imagecolortransparent.php

Tanoro
  • 871
  • 2
  • 10
  • 30