?
Solved

Resize image quality vs php version

Posted on 2003-02-23
8
Medium Priority
?
1,776 Views
Last Modified: 2012-06-27
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
Comment
Question by:petervullings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Accepted Solution

by:
3gg earned 200 total points
ID: 8008866
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
 
LVL 1

Expert Comment

by:Amerist
ID: 8008973
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
 
LVL 1

Author Comment

by:petervullings
ID: 8014001
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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 1

Expert Comment

by:Amerist
ID: 8015220
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
 
LVL 1

Expert Comment

by:3gg
ID: 8019083
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
 
LVL 1

Author Comment

by:petervullings
ID: 8022769
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
 

Expert Comment

by:semtex_81
ID: 13951154
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
 
LVL 1

Author Comment

by:petervullings
ID: 13991418
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question