PHP
  Home arrow PHP arrow Page 3 - An Image is Worth a Thousand Words in PHP Continued
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? 
PHP

An Image is Worth a Thousand Words in PHP Continued
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-12-21


    Table of Contents:
  • An Image is Worth a Thousand Words in PHP Continued
  • Examining the Code
  • Final Preparation
  • Converting An Image
  • In 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


    An Image is Worth a Thousand Words in PHP Continued - Final Preparation
    ( Page 3 of 5 )

    We're almost there! Just a few last knots to tie before we implement our conversion function. Let's go ahead and add in the rest of our class variables:

    # number of pixels (X-axis) to average together to determine a
    color
    var $skip_factor = 5;
     
    # store user-specified image information
    var $image_path = NULL;
    var $image_name = NULL;
    var $image_type = NULL;

    # store pointer to PHP image object
    var $img_obj  = NULL;

    # store image dimensions
    var $image_width = 0;
    var $image_height = 0;

    The above variables and comments are fairly self-explanatory, with the exception of 'skip_factor'. This variable simply specifies the number of pixels (along the X axis) we will be averaging in order to determine the color for each individual text character our class returns. The default value of 5 means that we will take the average value of every '5' pixels, and return exactly one text character. (Although this value is configurable, it should remain '5' for our application as changing it would require the CSS to be modified as well.)

    Next, let's add a method that allows our user to specify the location of the image file they wish to convert. (Remember, this can be a local or remote image - so long as a valid path has been provided.)

    # new load function - returns TRUE or FALSE to indicate success
    function load_image( $image_path ) {
     $this->image_path = $image_path;
     return ( $this->init_img() != FALSE );
    } # END load_image()

    And now for the last bit of preparation before we dive head-first into the conversion function:

    function init_img() {
     
     $this->image_name = end( explode( '/', $this->image_path ) );
     
     # determine type of file, and create it
     switch( end( explode( '.', $this->image_name ) ) ) {
      case 'jpg':
      case 'jpeg': $this->img_obj = @ imagecreatefromjpeg( $this-
    >image_path ); break;
      case 'png': $this->img_obj = @ imagecreatefrompng( $this-
    >image_path ); break;
      case 'gif': $this->img_obj = @ imagecreatefromgif( $this-
    >image_path ); break;
      case 'bmp': $this->img_obj = @ imagecreatefromwbmp( $this-
    >image_path ); break;
     }
     
     $this->image_width  = @ imagesx( $this->img_obj );
     $this->image_height  = @ imagesy( $this->img_obj );
     
     # return TRUE / FALSE to indicate success
     return ( $this->img_obj != '' );
     
    } # END init_img()

    The init() function, as you can see, simply checks the file extension of the user-specified image, and opens an image object for the appropriate file-type. It also stores the image dimensions within the class variables 'image_width' and 'image_height'.

    Both functions will return TRUE upon success or FALSE upon failure. If FALSE is returned, the user either specified an invalid image path, or an unsupported input format.



     
     
    >>> More PHP Articles          >>> More By Brian Vaughn
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT