1

I am uploading a image and its resizing, but on PNG, its showing black Background.

Can you please check the code and let me know what is the issue?

$newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
        if ($ext == "jpg" || $ext == "jpeg")
        {
            $source = imagecreatefromjpeg($image);
        }
        else
        if ($ext == "png")
        {
            $source = imagecreatefrompng($image);
        }
        else
        {
            $source = imagecreatefromgif($image);
        }

        imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
        imagejpeg($newImage,$image,90);
        chmod($image, 0777);
        return $image;
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
DeDevelopers
  • 581
  • 1
  • 7
  • 25

2 Answers2

1

Answer:

Added this code before imagecopyresampled() function

$tmp = imagecreatetruecolor($new_width,$new_height);
imagefilledrectangle($tmp, 0, 0, $new_width, $new_height, imagecolorallocate($tmp, 255, 255, 255));

and its start working as i want....

DeDevelopers
  • 581
  • 1
  • 7
  • 25
0

I got the same issue, I add the following code using alpha coloring from http://php.net/manual/en/function.imagecolorallocatealpha.php

//setting transparent color
  $color = imagecolorallocatealpha($this->imageResized, 0, 0, 0, 127);
//seting the image fill to the transparent color
  imagefill($this->imageResized, 0, 0, $color);
//saving the image with transparency before resizing
  imagesavealpha($this->imageResized, TRUE);
Jesus Quevedo
  • 71
  • 1
  • 3