Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1787
  • Last Modified:

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
0
petervullings
Asked:
petervullings
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now