Resize image quality vs php version

Hi there

A while back I coded a thumbnailer into my website so that my forum users could upload images. I use PHP/GD copy_image_resized, and although the resulting quality was not that good, it was ok.

Now all of a sudden, all new thumbs generated have really bad quality - almost like the palette is now restricted to 8 or 16 colours.  I have not touched the code in anyway, and the uploaded images are in exactly the same format. What I think has happened is that the PHP version on the server has changed (it is now 4.2.3. I dont know what it was previously).

Are there any known issues with new versions of PHP or GD that degrade the image quality when using copy_image_resized, or that now require you to enter the number of colours in the palette?

Cheers,
Peter
LVL 1
petervullingsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

3ggCommented:
When you create the new image, make sure you use the php function imageCreateTrueColor() instead of the imageCreate() this will give you the full pallet, which is needed when you are resampling an image.

Full documentation on this function is available here:
http://www.php.net/manual/en/function.imagecreatetruecolor.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmeristCommented:
You should check and see what version of GD is being Used.  This page here:

http://bugs.php.net/bug.php?id=17263

Talks about a bug in PHP version 4.2.1 -> 4.2.3 in the GD imagecopyresized() function when paletized images are used (PNG and GIF)

Tell me, does this problem happen with JPG images or PNG or both?
0
petervullingsAuthor Commented:
My image upload only supports jpegs, and so far has only been tested on jpegs. A phpinfo() on the server shows I have GD '2.0 or higher'.

I dont know what the previous version of GD was. All I know is that at a certain point the quality just went really bad (same images)..

Any idea when the truecolor image support was implemented in GD?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

AmeristCommented:
Since you have GD version 2.0 or higher you should try using the imagecopyresampled() function in place of imagecopyresized.  It makes amazingly better quality thumbnails.  It's available in GD versions 2.0.1 and up.

Truecolor support was added in version 2.0.

Here's an excerpt from
http://www.boutell.com/gd/manual2.0.11.html

"Support for truecolor images! Version 2.0 can load truecolor PNGs with no loss of color information, and almost no loss of alpha channel information. Version 2.0 can also load truecolor JPEGs with as little loss as possible; however, bear in mind that JPEG is a lossy format, so repeated load/save cycles always reduce image quality. This is not a bug. To create a truecolor image from scratch, call the new gdImageCreateTrueColor function. The gdImageCreate function is still available to create palette images, and may also be referred to as gdImageCreatePalette. "
0
3ggCommented:
The imageCopyResampled() does do a nice job too. However, you still want to start with a highcolor target.

Something else to check is your imagejpeg() that your image quality isn't set to low otherwise you may be loosing something there as well.

quick recap:

$im_in = imagecreatefromjpeg ("filename.jpg);
$im_out = imagecreatetruecolor(200,100);
imagecopyresampled($im_out, $im_in, 0, 0, 0, 0, 200, 100, 400, 200);
imagejpeg($im_out, "filename_thumb.jpg", 45);
imagedestroy($im_out);
imagedestroy($im_in);

0
petervullingsAuthor Commented:
Thanks everyone for all the answers. Very helpfull!  

I tried 3gg's solution of using imageCreateTrueColor() and it worked very well and fixed the problem. Although why the old functions arent exactly backwards compatible are beyond me (also had the problem with rendering fonts to a PNG as opposed to a GIF when GIF support was dropped - much different - but thats another story :) ).

ALso thanks for all the comments on ImageCopyResampled. I was wondering about a good image resampler, as the 'resized' version just chunks the hell out of the image! I heard the best way was to pipe it out to imagemagik and let it do the resizing, but now that PHP has this function (imageCopyResampled) its a lot tidier to keep it all in the code!

Thanks again,
Peter
0
semtex_81Commented:
this code works for me when i uplaod jpeg images.. But it doesnt work with gif images. why?.
can u please mail me any comment @  java@bsdmail.com
0
petervullingsAuthor Commented:
Because GIF is contains a copyright compression type and GIF support in PHP is usually turned off. Also, perhaps the fact that GIF is paletted causes a problem?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.