Jump to content

Image output to png - black image


tommybfisher

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/243466-image-output-to-png-black-image/
Share on other sites

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')))

 

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.