The ImageManager component implements methods to edit pictures. Here JPEG and GIF are as well supported as PNG images if the PHP GDLib is available in the correct version. To use the ImageManager it must be included into the current application using
PHP code
before instanciating it. For image handling purposes, the two static methods
  • showImageAttributes() and
  • resizeImage()
are available.

1. Displaying image attributes

The method showImageAttributes() is intended to return the attributes of an image. It returns an associative array or the value of a dedicated attribute. Hence, the code
PHP code
import('tools::image','ImageManager'); $image = './images/my_new_dog.png'; echo printObject(ImageManager::getImageAttributes($image));
creates the output
Array ( [width] => 180 [height] => 104 [type] => png [mimetype] => image/png [bitdepth] => 8 )
If the second argument contains the name of the desired attribute, the return value contains the value of the attribute directly. Thus, the code
PHP code
import('tools::image','ImageManager'); $image = './images/my_new_dog.png'; echo ImageManager::getImageAttributes($image,'type');
creates the output
The following list describes the attributes available:
  • width: the width of the image
  • height: the height of the image
  • type: the type of the image
  • mimetype: the mime type of the image
  • bitdepth: the bitdepth of the image
  • colormode: the color mode (RGB or CMYK)

2. Resize images

In order to change the dimensions of an image, the resizeImage() method can be used. To achieve this, the source image and the target dimensions must be applied as arguments. If no target image is given or the param contains the value null, the resized image is flushed to STDOUT. As an optional parameter, the quality of JPEG images can be set. The following code snippet shows, how the ImageManager can be used to generate thumbnails:
PHP code
import('tools::image','ImageManager'); $image = './images/my_new_dog.png'; $picto = './images/my_new_dog_small.png'; $attributes = ImageManager::getImageAttributes($image); $width = round((int)$attributes['width'] / 10,0); // resize to 10% $height = round((int)$attributes['height'] / 10,0); // resize to 10% ImageManager::resizeImage($image,$width,$height,$picto);
If a JPEG image should be flushed to STDOUT after resizing, but the quality of the target image should be set to a value of 90, this can be done with the following code snippet:
PHP code
import('tools::image','ImageManager'); $image = './images/my_old_dog.jpg'; ImageManager::resizeImage($image,$width,$height,null,90);

3. Application sample: ImageResizer

A typical application case for the ImageManager ist the delivery of images with dynamic sizes via a front controller action. The following code depicts the content of the run() method of the action, that additionally caches images, that were already delivered:
PHP code
// include the necessary libraries import('tools::image','ImageManager'); // gather the relevant params from the action input $image = urldecode($this->input->getAttribute('image')); $ext = $this->input->getAttribute('ext'); $size = (int)$this->input->getAttribute('size'); $path = $this->input->getAttribute('path'); // check, if all attributes are there $imagePath = $path.'/'.$image.'.'.$ext; if($image == null){ trigger_error('[ShowImageAction::run()] No image name ("image") given in frontcontroller input params!',E_USER_ERROR); exit(1); } if($ext == null){ trigger_error('[ShowImageAction::run()] No image extension information ("ext") given in frontcontroller input params!',E_USER_ERROR); exit(1); } if(!file_exists($imagePath)){ trigger_error('[ShowImageAction::run()] The image "'.$imagePath.'" does not exist. Please check your action call concerning the path, the name and the extension of the image!'); exit(1); } // gather the image attributes $attributes = ImageManager::getImageAttributes($imagePath); // resize image if($size === 100){ $imageSource = file_get_contents($imagePath); } else{ // calculate the new width and height $width = round(($size / 100) * intval($attributes['width']),0); $height = round(($size / 100) * intval($attributes['height']),0); // get cache manager $cMF = &$this->getServiceObject('tools::cache','CacheManagerFabric'); $cM = &$cMF->getCacheManager('imageresizer'); // try to load the file from cache $cacheKey = md5($imagePath.$size); $imageSource = $cM->getFromCache($cacheKey); if($imageSource === null){ // resize image and catch the output ob_start(); ImageManager::resizeImage($imagePath,$width,$height); $imageSource = ob_get_contents(); ob_end_clean(); // write to cache $cM->writeToCache($cacheKey,$imageSource); } } // send header header('Content-Type: '.$attributes['mimetype']); header('Content-disposition: inline; filename="'.$image.'.'.$ext.'"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: '.strlen($imageSource)); // stream image and exit echo $imageSource; exit(0);


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.