imageCreate will create an image, but its palette will be very limited, so
when you try to copy your photo onto it, it will maintain the palette of the
original picture.  Obviously, this is not what you want, since the picture
comes out looking all grey and faded (probably 8-16 colors!).

What works for me is creating a blank image using imageCreate(), then saving
it as a temporary jpeg (using imageJpeg). This action will set the palette
of the image to a jpeg's palette (close to 16-bit or better colorspace).  If
you imageCopyResized over this temp.jpg (which you need to create whenever
you run the script to ensure sizing is perfect), it will come out with the
full palette of colors.

Pseudo code:
$dest_h = whatever your calculated destination height is;
$dest_w = whatever your calculated destination width is;

$src_h = height of original image;
$src_w = width of original image;

   // create the blank limited-palette image
$base_image = imageCreate($dest_w, $dest_h);

   // convert and save it to temp.jpg
imagejpeg($base_image, 'path/to/temp.jpg');

   // get the image pointer to the temp jpeg
$image = imageCreateFromJpeg('path/to/temp.jpg');

   // get the image pointer to the original image
$imageToResize = imageCreateFromJpeg('path/to/image_to_be_resized.jpg');

   // resize the original image over temp.jpg
   // since you have GD2, you could also use imageCopyResampled
imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $dest_w, $dest_h,
$src_w, $src_h);

   // values for output jpeg quality
$jpegQuality = 75;

   // create the resized image
imageJpeg($image, 'path/to/thumbnail_name.jpg', $jpegQuality);

Then all you have to do is a little cleanup with imageDestroy() and you
should have a nice looking thumbnail!

I usually add a line like this to the script to make sure everything went
        echo '<html><head><title>Thumbnail Generator</title>'
            .'</head><body><img src="path/to/thumbnail_name.jpg">'

It lets me see the quality of the thumbnail the second the script completes.

Try it - you'll get perfect thumbnails every time.  I used this technique
for all of the pictures at with GD1.6 - and all
of them actually had custom color profiles!  Works like a charm.

I like this method because I can be sure that the script won't break on
sites with older PHP and GD versions - which is an important consideration
when you're deploying applications to other sites...

However!  If you are sure it will only be run on a PHP4, GD2 system, you can
avoid all of the temp jpg madness altogether by just altering your code to
use imageCreateTrueColor() instead of imageCreate()!!  ;P  Easy as pie!


-----Original Message-----
From: Arcadius A. [mailto:[EMAIL PROTECTED]]
Sent: Sunday, August 25, 2002 4:11 AM
Subject: [PHP] GD lib. image quality!

I wrote a little script for generation thumbnails of larger images....
$origImage = imageCreateFromJpeg($sourcePath."/".$currentImageName);
    $thumbnail = imageCreate($thumbWidth,$thumbHeight);// create empty image

    imageJpeg($thumbnail, $targetPath."/".$thumbNamePrefix."_thumb.jpg"); //
Store it
    imageDestroy($thumbnail); // cleanup
    echo "<br>Image ".$targetPath."/".$thumbNamePrefix."_thumb.jpg"."
created successfully!";


then, I've noticed that the quality of the thumbnails created is very bad!
my "phpinfo()" page shows  "2.0 or higher" as GD version

So, I'm wondering whether I'm doing something wrong in my code or whether
there exist a better library to use with PHP ... a library able to generate
good quality JPG files...

Thanks in advance.


PHP General Mailing List (
To unsubscribe, visit:

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to