Home arrow PHP arrow Page 4 - An Image is Worth a Thousand Words in PHP Continued

Converting An Image - PHP

Picking up where we left off in part one, we will work on some more helper methods and our conversion method. By the time we are done, we will have a fully functional image to text converter.

TABLE OF CONTENTS:
  1. An Image is Worth a Thousand Words in PHP Continued
  2. Examining the Code
  3. Final Preparation
  4. Converting An Image
  5. In Conclusion
By: Brian Vaughn
Rating: starstarstarstarstar / 5
December 21, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: