Object-Oriented PHP - Concepts, Techniques, And Code

Page 59

OOPHP_02.book Page 39 Friday, May 5, 2006 2:25 PM

Because this variable has a default value, you can create a class instance in two different ways. To accept the default thumbnail size, create an object like so: $thumb = new ThumbnailImage("graphics/My_Picture.jpg");

In this case, the maximum dimension of the thumbnail will be the default value. To construct a thumbnail of different dimensions, do the following: $thumb = new ThumbnailImage("graphics/My_Picture.jpg", 250);

Assigning a default value to an argument to the constructor is simply a convenience for users of your class. NOTE

When assigning default values to arguments to a method, you may have as many default values as you wish. However, arguments without a default value should not follow those that have a default value; in this particular class, the $path variable should not follow the $thumbnailsize variable.

Internal Behavior—Private Methods So far you have seen only private data members, but here you encounter your first private method. Private methods relate to the internal workings of a class and can be invoked only from within the class itself. The method that performs the image reduction (see Listing 6-2) is a private method—createThumb— called from within the constructor. private function createThumb($thumbnailsize){ //array elements for width and height $srcW = $this->imageproperties[0]; $srcH = $this->imageproperties[1]; //only adjust if larger than max if($srcW > $thumbnailsize || $srcH > $thumbnailsize){ $reduction = $this->calculateReduction($thumbnailsize); //get proportions $desW = $srcW/$reduction; $desH = $srcH/$reduction; $copy = imagecreatetruecolor($desW, $desH); imagecopyresampled($copy,$this->image,0,0,0,0,$desW, $desH, $srcW, $srcH) or die ("Image copy failed."); //destroy original imagedestroy($this->image); $this->image = $copy; } } Listing 6-2: The createThumb method

T he T hu mb na il Im a ge C la s s

39


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.