Catalog 269 BASE

Catalog 269 BASE - Classes

===

Chapter 10: Catalog 269 BASE

There is always a base class on which the inherited classes are built (though it’s not always named Base). In this application, Base will contain a set of utilities that all of the other classes will make use of. The class is declared with the following statement:

class Base {

Now on to the properties. There are only three default properties. The following three come into play when dealing with images and thumbnails.

◆ $image_src; ◆ $thumb_src; ◆ $thumb_width

Other properties will be created dynamically in course of the methods. M ETHOD SQL_FORM AT( ) This method helps build update and insert queries.

function sql_format ($field) {

if (empty($this->$field)) {

return “null”; } elseif (is_numeric($this->$field)) {

return $this->$field; } else {

return “‘“.$this->$field.”’”; } }

For the sake of a query, a variable may be null, a numeric quantity, or text string. Each of the data types requires a slightly different format within a query. Strings must be surrounded by single quotes. Numeric files can have no quotes, and for nulls, this method will return the string “null” —with no single quotes sur- rounding it.

M ETHOD SET_IM AGE_SRC( ) This method both saves the uploaded image to the filesystem and creates the thumbnail.

270 Part IV: Not So Simple Applications

It is complex enough to break down section by section. First remember that the $file_src is the sole argument of the method call. $file_src is a string that will contain the path to the image. Most often, $file_src will be a string like “ images/image_product_8_style_7”.

function set_image_src ($file_src=””) {

if (!empty($this->imagefile) && $this->imagefile != “none”) {

if ($file_src === “”) {

$file_src = “images/”.uniqid(“image_”); } umask(2); $sizearr = GetImageSize($this->imagefile);

Here, imagefile is a file that has been uploaded by a user. If a file itself exists but no file name is indicted by the $file_src argument, a variable called $file_src is created. Using the uniqueid() function, we create a file name that starts with “ image_” and then contains some random characters. We want to make sure file names are unique, so we don’t end up accidentally overwriting existing files. After that the umask is set (see your Unix man page for a description of umask), and then we use the getimagesize() function. This function returns an array with four ele- ments: the first is the image height, the second is width, the third is the image type (1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF), and the fourth is the height and width in a string that is ready for the <img> tag. This information is really helpful in that the script will be able to assign a correct extension, even if the supplied filename had a faulty extension, or none at all.

if ($sizearr[2] == 1) { $file_ext = “.gif”; } elseif ($sizearr[2] == 2) { $file_ext = “.jpg”; } elseif ($sizearr[2] == 3) { $file_ext = “.png”; } else {

$file_ext = strtolower( substr($this->imagefile_name

, strrpos($this->imagefile_name,”.”)

Using the information in the third element of the array, we determine the appro- priate extension. If the file type doesn’t match any of the possible values returned by getimagesize() , we parse the string containing the uploaded file’s name. We take the characters from the final dot on, make them lowercase, and assign them to $file_ext .