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!