resize corrupts image, turns it blue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mhfein
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 12:32 pm

resize corrupts image, turns it blue

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: resize corrupts image, turns it blue

Post by requinix »

Try using imagecreatetruecolor instead of imagecreate.
mhfein
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 12:32 pm

Re: resize corrupts image, turns it blue

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: resize corrupts image, turns it blue

Post by requinix »

Truecolor images are more accurate than palette images. Like comparing GIFs with PNGs.
I don't really know - just try it.
mhfein
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 12:32 pm

Re: resize corrupts image, turns it blue

Post by mhfein »

I tried it again and it worked.
Thanks very much for the idea!
MH
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: resize corrupts image, turns it blue

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: resize corrupts image, turns it blue

Post 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.
mhfein
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 12:32 pm

Re: resize corrupts image, turns it blue

Post by mhfein »

jedi mod,
Thanks for your clarification...
mh
Post Reply