GD and Image Manipulation

I got all fired up at the meeting about doing nifty things with GD and image metadata. Thus, I thought I'd start a thread.

Image Resizing

This is an extremely simple snippet of code to resize a given JPG or GIF.
See the kinds of thumbnails it outputs on my site [url]http://www.lyza.com[/url]. This is where I'll start!

I highly encourage input about how to increase quality.

The most important thing to note is the use of imagecreatetruecolor(). From the PHP site regarding imagecopyresized():

Note: There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().

[code:1]
/***********************************************************************/
/* resizeImage($src, $type, $end_width)
* Creates an image that is a re-sized image of $src, of type $type,
* and width $end_width.
*
* @src Source image resource
* @type image/jpeg or image/gif
* @end_width Width in pixels of desired resized image
* @return Image resource for resized image
*/
function resizeImage($src_image, $type, $end_width)
{
if($type == 'image/jpg' || $type == 'image/jpeg')
{
$src = imagecreatefromjpeg(
CELESTE_UPLOAD_PATH . "/$src_image")
or die("Problem In opening Source Image");
}
else if($type == 'image/gif')
{
$src = ImageCreateFromGIF(CELESTE_UPLOAD_PATH
. $src_image) or die("Problem in opening source image");
}

$ratio = ImageSX($src) / $end_width;
$end_height = ImageSY($src) / $ratio;

$dest = imagecreatetruecolor($end_width, $end_height)
or die("Problem In Creating image");
ImageCopyResized( $dest, $src, 0, 0, 0, 0, $end_width, $end_height,
ImageSX($src), ImageSY($src)) or die("Problem In resizing");

return $dest;
}
[/code:1]

Image MetaData

I'm also very interested in the harvest and manipulation of metadata in images. I'm curious to see whether using this library: [url]http://www.ozhiker.com/electronics/pjmt/index.html[/url] is useful. It claims to be able to deal with pretty much any kind of metadata one can think of.

The PHP exif functions are OK, also.

gd resize

thanks lyza,

It took a little experimenting. Here is what I have found.

Using imagecopyresampled instead of imagecopyresized cleans it up a bit. Also the default quality of imagejpeg is @75. You can visually see distortion at this setting. Bumping it up to 100 produces a very quality image but increases the file size dramatically. Setting the quality at 90 yields a nice looking image and keeps the file size reasonable.

EXIF Thumbnails

Here ( [url]http://www.hugsan.com/EXIFutils/[/url] ) is the EXIF Utilities package I mentioned last night. It works well. Though I haven't tried many others and am not nessecarily saying it's the "Cream Of The Crop" or anything. It does what I need.

I use that program to insert an EXIF thumbnail into the actual picture files themselves. This saves processor time compared to rendering thumbs on the fly (which has to be done with each page hit) and disk space compared to storing the thumbs in a seperate file (note: technically the image data is the same no matter where you store it, but it may now be filling unused disk space already dedicated to the existing file rather than taking additional disk space as a seperate file (further note: this is not going to be a terribly significant savings in disk space, this will not solve your disk space woes)). The main advantage though is the integrity of the relationship between thumbnail and picture. You cannot have a thumbnail for a picture that cannot be found. When you delete the picture, you delete the thumbnail (there can be only one).

On the other hand, using seperate files for thumbnails has some advantages as well. It's much less work (the value of simplicity cannot be overstated), you are not limited to image formats which support EXIF information and it's less disk intensive as you only need to read the thumbnail file from disk.

I'm very pleased with the way the Exif gig is working out myself. PHP has the function [url]http://www.php.net/manual/en/function.exif-thumbnail.php[/url] to pull the thumbnail image from the header.

Happy to discuss further if anyone is interested in doing this.

Mark

Re:GD and Image Manipulation

ARGH! Ignore my reference to a global variable I have defined within my own framework (CELESTE_UPLOAD_PATH). This should be replaced with an appropriate, safe upload path owned by the php user on your own site.