Home arrow MySQL arrow Page 4 - Online Photo Album Development using PHP and GD: Part 1

To Gif or Not to Gif - MySQL

This article is the first part of a four part series about saving money and space when it comes to showing off your pictures. Frank will be using PHP, MySQL and GD in this series.

TABLE OF CONTENTS:
  1. Online Photo Album Development using PHP and GD: Part 1
  2. Photo Sizing
  3. Photo Class
  4. To Gif or Not to Gif
  5. Resizing Images
  6. Conclusion
By: Frank Manno
Rating: starstarstarstarstar / 36
April 22, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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:

Many have asked whether gd will support creating GIF files again, since we have passed June 20th, 2003, when the well-known Unisys LZW patent expired in the US. Although this patent has expired in the United States, this patent does not expire for another year in the rest of the world. Since I have no way of limiting distribution of GIF-creating code to US users only that is guaranteed to please somebody else's lawyer, I have opted to follow the same policy that the ImageMagick authors are following: GIF creation will not reappear in GD until the patent expires world-wide on July 7th, 2004. I realize this situation is frustrating for many; please direct your anger and complaints toward the questionable patent system that allows the patenting of such straightforward algorithms in the first place. Thank you!

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.

[/Note]

/*****
* Retrieves size of original image. Sets the conversion scale for both * the thumbnail and resized image
*/
function getSize(){
if ($this->img){
$this->width = imagesx($this->img);
$this->height = imagesy($this->img);
$this->thumbscale = min(MAX_WIDTH / $this->width, MAX_HEIGHT / $this->height);
} else {
return false;
}
return true;
}

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.


/*****
* Creates a thumbnail image from the original uploaded image
*/
function setThumbnail(){
// Check if image is larger than max size
if ($this->thumbscale < 1){
$this->new_width = floor($this->thumbscale * $this->width);
$this->new_height = floor($this->thumbscale * $this->height);
// Create temp image
$tmp_img = imagecreatetruecolor($this->new_width, $this->new_height);
// Copy and resize old image into new
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->width, $this->height);
$this->thumb = $tmp_img;
}
return true;
}

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
$tmp_img = imagecreatetruecolor($this->new_width, $this->new_height);

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);
$this->thumb = $tmp_img;

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)
0 --> Starting y co-ordinate of the destination image (thumbnail)
0 --> Starting x co-ordinate of the source image
0 --> Starting y co-ordinate of the source image
$this->new_width --> Width of the thumbnail
$this->new_height --> Height of the thumbnail
$this->width --> Width of the source image
$this->height --> Height of the source image

We then assign the temporary image ($tmp_image) to our thumbnail object ($this->thumb).

/*****
* Resizes uploaded image to desired viewing size
*/
function resizeImage(){
if ($this->width < RESIZE_WIDTH){
$this->resize = $this->img;
return true;
} else {
// Create re-sized image
$tmp_resize = imagecreatetruecolor(RESIZE_WIDTH, RESIZE_HEIGHT);
// Copy and resize image
imagecopyresized($tmp_resize, $this->img, 0, 0, 0, 0, RESIZE_WIDTH, RESIZE_HEIGHT, $this->width, $this->height);
imagedestroy($this->img);
$this->resize = $tmp_resize;
return true;
}
}

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);



 
 
>>> More MySQL Articles          >>> More By Frank Manno
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: