SunQuest
 
       MySQL
  Home arrow MySQL arrow Page 3 - Online Photo Album Development using P...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway