HomePHP Page 3 - An Image is Worth a Thousand Words in PHP
Setting Up Our Class - PHP
This article, the first of two parts, describes a fun little project that will help you sharpen your image manipulation skills. The completed application takes an image and converts it to a string of text that resembles the original image. Brian Vaughn gets you started.
Since our program will be working with images, which may easily be quite large, it is a good first step for us to increase the maximum script-execution time allotted for our PHP page. The default amount of time a PHP script is allowed to execute is 30 seconds. For our example application, we will probably be well under that limit. Still, just to be on the safe side let’s increase it to a minute. To do that, let’s add the following constructor to our class:
# constructor: allow for longer max-execution script time function img_to_text() { set_time_limit ( 100 ); } # END img_to_text()
Let’s also add a few attributes and their associated helper ‘set’ methods to our class:
# set default configurable values for class var $char_mode = 'NORMAL'; var $chars = 'IMGTOTEXT'; var $return_format = 'COLOR'; var $aspect_ratio = 1;
# used to track position of current char in $chars string # (only if user has selected ‘NORMAL’ char order mode) var $char_index = 0;
# designates whether to display chars in 'random' or 'ascending' order function set_char_mode( $char_mode ) { $this->char_mode = ( strtoupper( $char_mode ) == 'RANDOM' ) ? 'RANDOM' : 'NORMAL'; } # END set_char_mode()
# chars used for return text-image (not applicable for 'matrix' return format) function set_chars( $chars ) { $chars = str_replace( ' ', '', $chars ); $this->chars = ( strlen( $chars ) > 0 ) ? $chars : $this->chars; } # END set_char_mode()
# return format for our image: 'color' (default), 'grayscale', 'monochrome', or 'matrix' function set_return_format( $return_format ) { $this->return_format = strtoupper( $return_format ); } # END set_return_format()
# specify image aspect ratio (0.5 = half size, 1 = the same size, 2 = twice size, etc) function set_aspsect_ratio( $aspect_ratio ) { $this->aspect_ratio = $aspect_ratio; } # END set_aspsect_ratio()
Each of the above attributes and methods pertain to a specific area of our ‘img_to_txt’ class. Although they are pretty straightforward, we’ll take a brief look at each before proceeding. The first, ‘set_char_mode’, will allow users to specify whether they want the text that will comprise the converted picture to be printed in a linear order, or randomly. The second and related function, ‘set_chars’, allows the user to specify the text they want their image to be made of.
For instance, if we called ‘set_chars’ and passed the string “THIS IS A PICTURE” – our resulting image would be made up of the repeating phrase, “THISISAPICTURE”. However, if we then called ‘set_char_mode’ and passed it the string “random”, the resulting image would be a random arrangement of the letters “ACEHIPRSTU” (the letters found within the string, “THISISAPICTURE”). You may notice that before we set the allowable characters within the ‘set_chars’ function, we removed any blank space from the user’s string. This is because an empty space character is of very little use to our program, (which the exception of the ‘monochrome’ format), so it is best to remove any spaces up front.
Next, we have the ‘set_return_format’ function. This allows users to choose an output format for their images. The default mode is ‘color’, but other possible modes are ‘grayscale’, ‘monochrome’, and ‘matrix’. We’ll see what each of these modes looks like in a bit, so for now we’ll move on.
The last function we just added is called ‘set_aspect_ratio’. This function allows users to choose the size of their text-only image relative to the size of their original image. (An aspect ratio of 0.5 would result in a converted image that’s half of the size of the original, and so on.)