HomePHP 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.
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' );
# 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();