tommybfisher Posted August 1, 2011 Share Posted August 1, 2011 I have a thumbnail creation script which works fine for shrinking jpg's and resaving them in their original format. But when I use the script to shrink a png and then save the thumbnail as a png, it just saves as solid black. I can't seem to find out why it is doing this. Is it something to do with the transparency layer? // Resample $image_p = imagecreatetruecolor($width, $height); if($type == "jpg" || "JPG" || "jpeg" || "JPEG") $image = imagecreatefromjpeg($final); elseif($type == "png" || "PNG" ) $image = imagecreatefrompng($final); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output if($type == "jpg" || "JPG" || "jpeg" || "JPEG") imagejpeg($image_p,$thisdir."/uploads/thumbs/tb_".$filename.".".$type, 100); elseif($type == "png" || "PNG" ) imagepng($image_p,$thisdir."/uploads/thumbs/tb_".$filename.".".$type, 100); Quote Link to comment https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/ Share on other sites More sharing options...
Adam Posted August 1, 2011 Share Posted August 1, 2011 It's down to the expression in your IF statement: if($type == "jpg" || "JPG" || "jpeg" || "JPEG") $type == is not carried on into the next condition. You're effectively performing a boolean check on each of those strings which will all return true. Only if a string is empty will a booleach check on it return false. An easier way to demonstrate this is: if ('false') { echo 'evaluates to true..'; } Contrary to what you might expect, the echo statement is executed here. In your case the "JPG" string is checked and evaluates to true, so even if you have a PNG image you're outputting a JPG. You should have used: if($type == "jpg" || $type == "JPG" || $type == "jpeg" || $type == "JPEG") But to keep the code cleaner you could use an array and convert the type to lower-case: if (in_array(strtolower($type), array('jpg', 'jpeg'))) Quote Link to comment https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/#findComment-1250159 Share on other sites More sharing options...
tommybfisher Posted August 1, 2011 Author Share Posted August 1, 2011 Thanks for the reply, very clear and it really helped. I've now corrected the IF statement further up in the code (as I was repeating this same mistake several times). So now for the image resample and save, it is just working on an IF with no or's. My problem now is that now the png outputs as a zero kb file (jpg's are still outputting fine). Any ideas as to why this is happening? // Resample $image_p = imagecreatetruecolor($width, $height); if($type == "jpg") $image = imagecreatefromjpeg($final); elseif($type == "png") $image = imagecreatefrompng($final); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output if($type == "jpg") imagejpeg($image_p,$thisdir."/uploads/thumbs/tb_".$filename.".".$type, 100); elseif($type == "png") imagepng($image_p,$thisdir."/uploads/thumbs/tb_".$filename.".".$type, 100); Quote Link to comment https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/#findComment-1250182 Share on other sites More sharing options...
Adam Posted August 2, 2011 Share Posted August 2, 2011 I can't spot anything wrong with it. Try debugging the process and variables - it could be something very simple. Quote Link to comment https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/#findComment-1250560 Share on other sites More sharing options...
tommybfisher Posted August 3, 2011 Author Share Posted August 3, 2011 I've got it fixed. I had png compression set to 100, didn't realise png compression only goes from 0 - 9. Many thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/#findComment-1251232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.