简体   繁体   中英

imagecreatefrompng fails with 24bit pngs using PHP GD library

I'm trying to build a script that will resize a png and save it to the server as an 8bit png, from a <input type='file' /> but I am having a few issues.

$srcimage = imagecreatefrompng($orig_source);
$img = imagecreatetruecolor($resize_width, $resize_height);
$bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagecolortransparent($img, $bga);
imagefill($img, 0, 0, $bga);
imagecopy($img, $srcimage, 0, 0, 0, 0, $resize_width, $resize_height);
imagetruecolortopalette($img, false, 255);
imagesavealpha($img, true);
imagepng($img, $filepath);
imagedestroy($img);

How to convert PNG to 8-bit PNG using PHP GD library

This works great when i upload a 32bit png, and when i upload an already 8bit png, it keeps the transparency and resizes as expected, but when I choose to upload a 24bit png I get this error

Warning: imagecreatefrompng() [function.imagecreatefrompng]: '/share/MD0_DATA/Qweb/php2zRiNv' is not a valid PNG file

Warning: imagecopy() expects parameter 2 to be resource, boolean given

I understand the second error is because the imagecreatefrompng failed, but I don't understand why that should fail as it is a valid png file!

Any help would be greatly appreciated!

如果要从Photoshop等应用程序中保存它,请确保使用“另存为Web”选项,因为某些应用程序嵌入了非标准数据(如指南)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM