PHP调整大小png图像添加黑色背景

时间:2018-04-04 16:22:19

标签: php

当我尝试调整PNG图像的大小时,我遇到了一个问题,它总是在图像上添加黑色背景而不是白色。

这是我的代码示例(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);

?>

0 个答案:

没有答案