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 Let’s also add a few attributes and their associated helper ‘set’ methods to our class: # set default configurable values for class # used to track position of current char in $chars string # designates whether to display chars in 'random' or 'ascending' order 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.)
blog comments powered by Disqus |