MySQL
  Home arrow MySQL arrow Page 4 - Online Photo Album Development using PHP and GD: Part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Online Photo Album Development using PHP and GD: Part 1
By: Frank Manno
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 36
    2004-04-22


    Table of Contents:
  • Online Photo Album Development using PHP and GD: Part 1
  • Photo Sizing
  • Photo Class
  • To Gif or Not to Gif
  • Resizing Images
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Online Photo Album Development using PHP and GD: Part 1 - To Gif or Not to Gif
    ( Page 4 of 6 )

    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
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek