PHP
  Home arrow PHP arrow Page 4 - 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 - Converting An Image
    ( Page 4 of 5 )

    Now it's time to take a look at the meat of our application: the conversion process. It's a lot of code at first glance, but we'll break it into smaller sections. First, let's add the function to our class:

    function get_image() {
     $text_image = NULL;
     $switch   = FALSE;
     
     # init image, if not already setup
     if ( !isset( $this->img_obj ) )
      if ( !$this->init_img() )
       return NULL;
     
     for( $y = 0; $y < $this->image_height; $y += ( $switch ) ? 2 :
    1 ) {
      $switch = !$switch;
      $red  = 0;
      $green = 0;
      $blue  = 0;
      
      for( $x = 0; $x < $this->image_width; $x++ ) {
      
       // retrieve current pixel color info
       $rgb_color = imagecolorat( $this->img_obj, $x, $y );
       $rgb_color = imagecolorsforindex( $this->img_obj,
    $rgb_color );
       $red    += $rgb_color['red'];
       $green   += $rgb_color['green'];
       $blue    += $rgb_color['blue'];
       
       # average colors and output a block
       if ( $y % $this->skip_factor == $this->skip_factor - 1 && $x %
    $this->skip_factor == $this->skip_factor - 1 ) {
        $red  /= $this->skip_factor;
        $green /= $this->skip_factor;
        $blue  /= $this->skip_factor;
        
        # if monochrome - the 'color' is actually the char, (the real
    color is always white
        if ( $this->return_format == 'MONOCHROME' ) {
         $hex_color = 'FFFFFF';
         $char    = $this->rgb_2_monochrome( $red, $green, $blue );
         
        # if matrix mode, get a green color and a special char
        } else if ( $this->return_format == 'MATRIX' ) {
         $hex_color = $this->rgb_2_matrix( $red, $green, $blue );
         $char = $this->get_matrix_char();
         
        # otherwise, get appropriate color & leave the char alone
        } else if ( $this->return_format == 'GRAYSCALE' ) {
         $hex_color = $this->rgb_2_grayscale( $red, $green, $blue );
         $char = $this->get_char();
        } else {
         $hex_color = $this->rgb_2_color( $red, $green, $blue );
         $char = $this->get_char();
        }
        
        # output colored char
        $text_image .= "<font color='#$hex_color'>$char</font>";
       
        # re-set color totals for block
        $red  = 0;
        $green = 0;
        $blue  = 0;
     
       } # END average colors
      } # END foreach-y ($b)
      
      # output a new line if appropriate
      if ( $y % $this->skip_factor == 1 ) $text_image .= '<br>';
      
     } # END foreach-x ($a)
     return '<div ' . $this->get_css() . '>' . $text_image .
    '</div>';
     
    } # END get_image()

    The above code can be split into four main sections: checking to make sure our image has been set up properly, walking through the image and retrieving color information, converting the information we have retrieved into colored text, and finally, returning the text we have created, complete with browser-specific CSS code, to be displayed.

    The first step is rather easy. If an image has not been set up correctly, our function will return a NULL string, indicating failure.

    Next our code enters a series of for loops. The outer loop walks through each pixel on the Y axis, and the inner walks through each pixel on the X axis. Using these loops, our function traverses each pixel of the specified image and collects information about the color of each pixel. This information is temporarily stored in loop variables 'red', 'green', and 'blue' and averaged every 'skip_factor' number of times through the loop. Once each skip factor is reached, and the colors have been averaged, our function checks for the user specified return format and uses our helper color and character functions in order to append the appropriate character to our return string.

    Finally, once our function has completed its conversion loops, it calls out to 'get_css' to retrieve the appropriate browser-specific styling information. Without this information our image would still be recognizable, but not as pretty. And that's it.



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