In the huge set of library, Zend Framework have no options for manipulating image. There is a proposal for Zend_Image which aimed to provides easier image manipulation using GD. But the proposal was not considered. Bill Karwin of ZF said,
Proposal not being considered
This proposal has been reviewed. The proposal describes functionality that is very similar to the existing PHP interfaces for ImageMagick or GD.
We are not considering this for inclusion in the Zend Framework at this time.
So, what to do when we need to make image thumbnail, resizeing or cropping image etc in Zend Framework?
The Solution
Zend Framework has the extensive power of integrating any library. So, we can use ImageMagick or GD library or any wrapper class using this libraries for manipulating image.
Here, I am explaining creating image thumbnail using PHP Thumbnailer Class v2.0 in 2 easy steps. I am assuming that, you are using Zend’s MVC with common directory structure and you’ve GD library with your PHP installation.
Step 1 – Create a Model for image manipulation :
Download the PHP5 version of PHP Thumbnailer Class v2.0 and put the class file “thumbnail.inc.php” in your model directory. Now change the file name to “Thumbnail.php” as it’s class name. Now your model is ready for action.
Step 2 – Use this model in Controller :
In your Controller, write a function to make thumbnails using this model. For example, you can use this function below:
1: /**
2: * Create thumbnail of an image
3: *
4: * @author Anis uddin Ahmad (http://ajaxray.com)
5: * @param string Path of source image
6: * @param string Destination path of thumbnail
7: * @param number Width of thumbnail
8: * @param number Height of thumbnail
9: * @param number (optional) thumbnail image quality
10: */
11: private function _createThumbnail($sourcePath, $destPath, $w, $h, $q = 100)
12: {
13: $thumb = new Thumbnail($sourcePath);
14: $thumb->resize($w, $h);
15: $thumb->save($destPath, $q);
16: }
Done. Now you can use this function to resize/make thumbnail of an image. To resize an image with keeping it’s original name, pass the $sourcePath again in $destPath parameter. Then it will overwrite the existing image.
So, was it so though to manipulate image in ZF?