In earlier versions of GD, multiple image types were supported, including GIF, JPEG, PNG, and others. Due to patents held by the Unisys LZW group, GIF support has been discontinued in order to keep GD a free product. However, for those of you who are looking for GIF conversion support, the GD website states that GIF support will be reappear in June 2004; the date in which the patent will expire world-wide:
GD version 2.x supports image conversion for JPEG, PNG, and WBMP. WBMP is supported by wireless browsers. For this article, however, we'll focus primarily on JPEG and PNG image formats. Adding support for WBMP images is as easy as adding in calls to the GD functions which support conversion for WBMP files.
The getSize() function uses GD's imagesx() and imagesy() functions to retrieve the width and height of the image. We then perform a division calculation to determine the scale for the thumbnail images:
min(MAX_WIDTH / $this->width, MAX_HEIGHT / $this->height);
PHP's min() function returns the lowest value of all arguments passed to it. By performing a min() calculation, we are able to determine the scaling ratio used to resize our image. We use the lowest value of the two arguments to maintain the aspect ratio when reducing the image size; otherwise, we would end up with a distorted image.
The setThumbnail() function first checks to see if the thumbscale (set by the getSize() function), is less than 1. If the scale is larger than 1, the original image is less than the desired thumbnail size, and this function is skipped completely. If, however, the scale is less than 1, we resize the image to the desired thumbnail size (in this case 100x100). Remember, however, that since we took the lower of the two values when determining the conversion scale, GD will resize the thumbnail appropriately, so the image won't actually be 100x100.
$this->new_width = floor($this->thumbscale * $this->width); $this->new_height = floor($this->thumbscale * $this->height);
By using the thumbscale variable, we can determine the exact size to use when creating our thumbnail. This will maintain the aspect ratio of our image.
// Create temp image
imagecreatetruecolor() creates a pointer to a true-color image, set the size of our thumbnail's dimensions, calculated above.
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->width, $this->height);
We then call imagecopyresampled() passing to it the $tmp_img pointer, the original image, as well as the following:
0 --> Starting x co-ordinate of the destination image (thumbnail)
We then assign the temporary image ($tmp_image) to our thumbnail object ($this->thumb).
resizeImage() does just that. We will perform the resizing of the original image to the specified width and height specified in our defined constants. We first check to see if it is necessary to resize the image. Because there is the possibility that an image may be longer than it is wide, and still be less than our desired RESIZE_WIDTH (ie: 800px). We verify to see if this is so, and simply use the existing image.
If the image is larger than the desired width, we'll resize it:
$tmp_resize = imagecreatetruecolor(RESIZE_WIDTH, RESIZE_HEIGHT);
blog comments powered by Disqus