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

An Image is Worth a Thousand Words in PHP Continued

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

Ramp Up

Welcome back! We are about to pick up where we left off in part one, but first a quick summary of what we have done so far. We began by creating our conversion class and naming it 'img_to_txt'. Inside this class we defined several basic accessory methods, to allow our users a variety of options. Last, we also set up a method for returning the appropriate CSS markup based on the user's browser type.

That's what we've done so far - but there's a lot more to do, so let's get started. If you have not yet read part one of "An Image is Worth a Thousand Words," you should read through it before continuing.

Characters and Colors

We're almost ready to start working on our conversion method, but there are still a few associated, helper methods that we should define before proceeding. We can split them into two main categories: characters and colors. Let's take a closer look at each.

As we've already mentioned, our 'img_to_txt' class will allow a user to specify a custom set of characters with which to build our converted image. These characters can be printed in ascending order (i.e. "THISISASTRING") or in random order (i.e. "IAHTISAGEHG"). Furthermore, certain output modes ("matrix" for example) will be comprised of a separate, custom set of chars. To keep things clean, we can split this logic of the application into a couple of helper 'get' and 'init' methods:

 # return next char for 'color' and 'grayscale' output modes
 function get_char() {
  if ( $this->char_mode == 'NORMAL' )
   return $this->chars[ ( $this->char_index++ % strlen( $this-
>chars ) ) ];
  else
   return $this->chars[ rand( 0, strlen( $this->chars ) - 1 ) ];
 } # END get_char()
 
 # return next char for 'matrix' output mode
 function get_matrix_char() {
  return $this->matrix_chars[ rand( 0, strlen( $this-
>matrix_chars ) - 1 ) ];
 } # END get_matrix_char()

The above functions simply return a single char at a time, depending on the output mode selected. If 'color' or 'grayscale', the function also checks to see if the user has specified a char_mode (of 'NORMAL' or 'RANDOM') and acts accordingly. (Some of you may have noticed that the functions listed only deal with 'color', 'grayscale', and 'matrix' modes. This is because our 'monochrome' mode requires a slightly different approach, but we'll get into that shortly.)

Our application also allows a user to convert an image into one of several output modes (color, grayscale, monochrome, etc.). Because of this, we should probably split this area of logic into a few 'get' helper functions. Let's list them below, then take a closer look at each:

 # specify list of all possible HEX values, in ascending order,
and store in array
 var $hex_array   = array();
 var $hex_values   = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' );
 
 # specify list of all possible monochrome values, in ascending
order, and store in array
 var $monochrome_values = ' .:xW©';
 var $monochrome_array  = array();
 
 # specify list of all possible 'matrix' chars
 var $matrix_chars  =
'¶µ®@&„$£«#©§¦';
 # initializes (builds) the HEX color array
 function init_hex_array() {
  for( $a = 0; $a < count( $this->hex_values ); $a++ )
   for( $b = 0; $b < count( $this->hex_values ); $b++ )
    array_push( $this->hex_array, $this->hex_values[ $a ] .
$this->hex_values[ $b ] );
 } # END init_hex_array() 

 # initializes (builds) the monochrome color array
 function init_monochrome_array() {
  for( $a = 0; $a < strlen( $this->monochrome_values ); $a++ )
   array_push( $this->monochrome_array, $this->monochrome_values
[ $a ] );
 } # END init_monochrome_array()
 
 # converts and returns a (color) HEX equivalent of an RGB color
value
 function rgb_2_color( $red, $green, $blue ) {
  return $this->rgb_2_hex( $red ) . $this->rgb_2_hex( $green ) .
$this->rgb_2_hex( $blue );
 } # END rgb_2_color()
 
 # converts and returns a (grayscale) HEX equivalent of an RGB
color value
 function rgb_2_grayscale( $red, $green, $blue ) {
  $rgb_avg = ( $red + $green + $blue ) / 3;
  $hex_avg = $this->rgb_2_hex( $rgb_avg );
  return $hex_avg . $hex_avg . $hex_avg;
 } # END rgb_2_grayscale()

 # converts and returns a grayscaled HEX equivalent of an 256-
color, index value
 function rgb_2_hex( $rgb_color ) {
  if ( count( $this->hex_array ) <= 0 ) $this->init_hex_array();
  
  $color_index = ( 255 / ++$rgb_color );
  $color_index = round( count( $this->hex_array ) /
$color_index );
  $color_index = ( $color_index >= count( $this->hex_array ) ) ?
count( $this->hex_array ) - 1 : $color_index;
  
  return $this->hex_array[ $color_index ];
 
 } # END rgb_2_hex()
 
 # converts and returns a (green) HEX equivalent of an RGB color
value
 function rgb_2_matrix( $red, $green, $blue ) {
  $rgb_avg = ( $red + $green + $blue ) / 3;
  return '00' . $this->rgb_2_hex( $rgb_avg ) . '00';
 } # END rgb_2_matrix()
 
 # averages RGB colors passed, and returns a similarly weighted
character
 function rgb_2_monochrome( $red, $green, $blue ) {
  # init monochrome array if necessary
  if ( count( $this->monochrome_array ) <= 0 ) $this-
>init_monochrome_array();
  
  # determine character from MONOCHROME array to display
  $rgb_avg = ( $red + $green + $blue ) / 3;
  $char_index = ( 255 / ++$rgb_avg );
  $char_index = round( count( $this->monochrome_array ) /
$char_index );
  $char_index = ( $char_index >= count( $this->monochrome_array ) ) ? count( $this->monochrome_array ) - 1 :
$char_index;

  # return char
  return $this->monochrome_array[ $char_index ];
 } # END rgb_2_monochrome()



 
 
>>> 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: