0

I got an issue when i try to resize a PNG image, it always add me a black backgroud to the image instead of being white.

here is a sample of my code (PHP)

<?php

$url='https://vignette.wikia.nocookie.net/mario/images/d/d7/SMO_Art_-_Mario.png/revision/latest/scale-to-width-down/430?cb=20170129083448&path-prefix=fr';

$image_encode = base64_encode(file_get_contents($url));
$image_decode = base64_decode ( $image_encode );

$formImage = imagecreatefromstring ( $image_decode );

// imagepng($formImage,'simpletext.png'); //save image into file
 list($width_orig,$height_orig) = getimagesizefromstring ( $image_decode );

 $sizeX4 = $width_orig/4;
 $new_height = $height_orig/4;

 $resizeimage = imagecreatetruecolor ( $sizeX4, $new_height );
 imagealphablending($resizeimage, FALSE);
 imagesavealpha($resizeimage, TRUE);

 imagecopyresampled ( $resizeimage, $formImage, 0, 0, 0, 0, $sizeX4, $new_height, $width_orig, $height_orig );


 header('Content-Type: image/png');
 imagepng($resizeimage,'mario.png');

 imagedestroy($resizeimage);
 imagedestroy($formImage);

?>
Script47
  • 14,230
  • 4
  • 45
  • 66
  • @StefanCrain, yes but seems nobody found the solution :-( – Jean Fabre Apr 05 '18 at 13:05
  • I ran your code above in PHP 7.2.4 and I see a transparent background when I open it with my image editor. Opening it in Chrome shows a black background, Firefox a grey background, and Safari a white background. – Stefan Crain Apr 05 '18 at 13:38
  • The fact is when I saved it on my local desktop, I got this black background. Could you tell me if in PHP 7.2 you have the same issue. I'm on 5.6 – Jean Fabre Apr 05 '18 at 17:33
  • I've tested with PHP 5.6.35 with the same results. How are you verifying the background is black? – Stefan Crain Apr 05 '18 at 17:42
  • When I try save it in a folder in my desktop and open it as a picture. (not with my navigator). Thx Stefan. – Jean Fabre Apr 06 '18 at 10:27
  • OK i updated my php version and now it works ! Thx Stefan – Jean Fabre Apr 13 '18 at 08:26

0 Answers0