Page 1 of 1
resize corrupts image, turns it blue
Posted: Thu Oct 23, 2008 12:44 pm
by mhfein
Users upload images to a website and I store them in a folder. I also need to make a thumbnail, so I resize the image and put it in a different folder. Every so often, a thumbnail loses it color. It turns sort of blue and looks bad. I don't know why this happens. Any help greatly appreciated. Here is my relevant code:
The $image variable is a path to the uploaded image.
$new_width and $new_height are the dimensions I want for the new thumbnail.
$adid is just a number to use in naming the new image
I think everything else should be clear:
$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];
$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($new_width,$new_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,$new_width,$new_height,$width,$height);
$newDirectory = "gfx/images/";
$newFilename = $newDirectory . $adid . ".jpg";
imagejpeg($dst, $newFilename, 75);
ImageDestroy($src);
ImageDestroy($dst);
Thanks for any insights into what I may be doing wrong.
Mary Helen Fein
Re: resize corrupts image, turns it blue
Posted: Thu Oct 23, 2008 4:07 pm
by requinix
Try using
imagecreatetruecolor instead of imagecreate.
Re: resize corrupts image, turns it blue
Posted: Thu Oct 23, 2008 11:51 pm
by mhfein
I tried imagecreatetruecolor, but I got a bunch of warnings, so I backed off. I will try again. Can you explain what it does differently from the command I am using, ImageCreate? I would greatly appreciate that as the doc you linked to is not clear.
Re: resize corrupts image, turns it blue
Posted: Fri Oct 24, 2008 12:25 am
by requinix
Truecolor images are more accurate than palette images. Like comparing GIFs with PNGs.
I don't really know - just try it.
Re: resize corrupts image, turns it blue
Posted: Fri Oct 24, 2008 11:36 am
by mhfein
I tried it again and it worked.
Thanks very much for the idea!
MH
Re: resize corrupts image, turns it blue
Posted: Fri Oct 24, 2008 12:24 pm
by onion2k
tasairis wrote:Truecolor images are more accurate than palette images. Like comparing GIFs with PNGs.
Just because I'm an utter pedant and have to "correct" everything on here ... PNGs are a bit more complicated than that. They can be 8 bit (palette based) like a GIF, or 24 bit (3 8 bit channels like a JPEG, with a transparent colour like a GIF), or 32 bit (3 8 bit channels with an 8 bit alpha channel). imagepng() in PHP will create an 8 bit palette based image if the GD resource is 8 bit too (created with imagecreate(), or imagegif(), or imagepng() with an existing 8 bit image), or a 32 bit PNG if the GD resource is a true colour image.
Re: resize corrupts image, turns it blue
Posted: Fri Oct 24, 2008 12:57 pm
by requinix
onion2k wrote:tasairis wrote:Truecolor images are more accurate than palette images. Like comparing GIFs with PNGs.
Just because I'm an utter pedant and have to "correct" everything on here ... PNGs are a bit more complicated than that. They can be 8 bit (palette based) like a GIF, or 24 bit (3 8 bit channels like a JPEG, with a transparent colour like a GIF), or 32 bit (3 8 bit channels with an 8 bit alpha channel). imagepng() in PHP will create an 8 bit palette based image if the GD resource is 8 bit too (created with imagecreate(), or imagegif(), or imagepng() with an existing 8 bit image), or a 32 bit PNG if the GD resource is a true colour image.
Yeah... I'll have to concede that to you. I didn't really know what I was saying, I think it was more like an analogy than a comparison.
But I think I understand it now, so thanks.
Re: resize corrupts image, turns it blue
Posted: Fri Oct 24, 2008 3:31 pm
by mhfein
jedi mod,
Thanks for your clarification...
mh