DeDevelopers
DeDevelopers

Reputation: 591

PNG image showing black background on uploading/resizing

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;

Upvotes: 1

Views: 2152

Answers (2)

Jesus Quevedo
Jesus Quevedo

Reputation: 81

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);

Upvotes: 0

DeDevelopers
DeDevelopers

Reputation: 591

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....

Upvotes: 1

Related Questions