An Image is Worth a Thousand Words in PHP - Project Overview (
Page 2 of 4 )
Admittedly, the program we are about to develop may lack an immediate, real-world application. So rather than contrive a situation in which we, as developers, would be asked to create such an application, let’s simply talk about what we will be designing our program to do. The benefits of such an exercise will then become clear as we work through the various stages of development.
The basic goal of our application is simple: receive as a parameter a string designating the location of an image file (this can be a local image or a remote one), load the image, and convert it to a string of colored text that resembles the original picture. To go a step beyond that, we will also allow for multiple output modes including: color, grayscale, monochrome, and matrix. This will allow users to convert images to a variety of text-only formats. Below you’ll see a couple of ‘teaser’ sample images, displaying conversions in each of our various output formats:


We’re going to be using an object-oriented programming approach, so we will start off by creating a class to contain our image manipulation methods. Let’s call this class ‘img_to_txt’, and let’s store it in a file named ‘img_to_txt.php’. For now its contents should be as follows:
<?php
# used to convert images to text-only format
class img_to_txt {
} # END img_to_txt class
?>
Although there may not be an immediate need to re-use the methods we are about to create, it is still a good programming practice to make them as portable as possible. (After all, you never know when you may want to re-use something!) Because of this, we've chosen to bundle all of our code inside a simple helper class – but this class needs a few get-and-set methods in order to function properly. That’s what we’ll spend the first part of this article focusing on. Each of these methods will be pretty self-explanatory, and will simply allow us to define certain attributes of our image (such as its location, and the desired output format).