MySQL
  Home arrow MySQL arrow Page 3 - 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? 
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 - Photo Class
    ( Page 3 of 6 )

    Next we define our class:

    class GallerySizer{
    var $img; // Original image file object
    var $thumb; // Thumbnail file object
    var $resize; // Resized image file name
    var $width; // Original image width
    var $height; // Original image height
    var $new_width; // Resized image width
    var $new_height; // Resized image height
    var $image_path; // Path to image
    var $thumbscale; // Scale to resize thumbnail
    var $image_file; // Resized image filename
    var $thumbnail; // Thumbnail image file object
    var $random_file; // Resized image file name (random)

    To define a class file, you simply use the keyword class followed by the class name; in our case, GallerySizer. The convention behind class names is to capitalize the first letter of the class name, followed by capitalizing the first letter of every following word without underscores or dashes (ie: GallerySizer and not Gallery_Sizer).

    The variables we've created are known as member data, when discussing them in terns of Object Oriented Programming (OOP). These variables are global to the class, which will allow any method (function) to access them. They are used throughout the script for various functions, including creating the thumbnail, determining the resizable scale for the new images, creating a random filename for the newly converted images, etc.

    /*****
    * Retrieves path to uploaded image.
    * Retrieves filename of uploaded image
    */
    function getLocation($image){
    $this->image_file = str_replace("..", "/", $image);
    $this->image_path = IMAGE_BASE . $this->image_file;
    return true;
    }

    The method above, getLocation($image), accepts an image as its argument. The image will be passed from the upload form to the method, which will then initialize the $image_file variable to hold the name of the image, and the $image_path variable to hold the path to where the resized image will reside on the server.

    <span style="background-color: #ffff00;">/*****
    <br />* Determines image type, and creates an image object
    <br />*/
    <br />function loadImage(){
    <
    br />$this->img null;
    <
    br />$extension strtolower(end(explode('.'$this->image_path)));
    <
    br />if ($extension == 'jpg' || $extension == 'jpeg'){
    <
    br />$this->img imagecreatefromjpeg($this->image_path);
    <
    br />} else if ($extension == 'png'){
    <
    br />$this->img imagecreatefrompng($this->image_path);
    <
    br />} else {
    <
    br />return false;
    <
    br />}
    <
    br />// Sets a random name for the image based on the extension type
    <br />$file_name strtolower(current(explode('.'$this->image_file)));
    <
    br />$this->random_file $file_name $this->getRandom() . "." $extension;
    <
    br />$this->thumbnail $this->random_file;
    <
    br />$this->converted $this->random_file;
    <
    br />$this->resize $this->random_file;
    <
    br />return true;
    <
    br />}

    The loadImage() function above determines the file-type of the current image by splitting the filename into an array, split by the dot (.) in its name, using PHP's explode() function. The end() function simply retrieves the last element in the array.

    Based on the image type, we call the "imagecreatefromXXXX" function, which, in the case of a JPEG, returns a pointer to a true-color image. This pointer is used later on the code to create our resized and thumbnail images.

    To ensure that every image uploaded is unique, we retrieve the name of the image (less the extension), and add a random value to the image name. In this case, our getRandom() function will return the current date/time value, which will then be appended to the filename, creating a unique name.

    [Note]



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT