0

I have a transparent png image. i'm trying to resize it using a function.

function resize_image($image, int $newWidth, int $newHeight) {
    $newImage = imagecreatetruecolor($newWidth, $newHeight);
    imagealphablending($newImage, false);
    imagesavealpha($newImage,true);
    $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
    imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, $transparent);
    $src_w = imagesx($image);
    $src_h = imagesy($image);
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $src_w, $src_h);
    return $newImage;
}

But it still return a png with a colored background.

It seems it only works on transparent bakcground with opacity = 0. How can I solve the problem? I tried removing the two lines that reprocess the background, but it doesn't change anything.

FoxBall
  • 55
  • 5
  • 1
    Looks like you copy&pasted from https://stackoverflow.com/a/279310/1427878 ? Did you take note of the update below that, saying _"This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background."_? I am guessing yours is probably a case of the latter then. – CBroe Dec 14 '22 at 07:14
  • How can I solve the problem then? – FoxBall Dec 14 '22 at 12:30

0 Answers0