Image manipulation in Zend Framework using PHP Thumbnailer Class v2.0

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?

27 Comments

  1. That’s a really cool tip, but it seems that the location of some of the parts seems mixed up.

    I would place the thumbnail class in a library folder and place the custom function in an image manipulation model class. Then make a call to that function in the controller.

  2. Thanks very much for this. I’ve been learning Zend for the past few weeks, and going well with it, but this has just opened up new horizons for me. thank you.
    Going to try to add some other classes I use to Zend now this way too. thanks

  3. Thanks for the tipp. The library looks very interessting. I bookmarked WideImage, but I have never tested it. After the next project where I need one of these thumbnailer libraries, I will write about the best one 😉

    Regards
    Tobi

  4. Hi,
    I get an issue using Internet Explorer, both 6 and 7, thumbs are not created.
    No problem with firefox, any advices?
    Thanks

  5. Don’t worry, solved, it was caused by a wrong mime control, what for firefox is image/jpeg for ie is image/pjpeg.
    Bye

  6. Great tutorial! Short and sweet.

    The latest version of PHPThumb (3.0) is at:
    http://phpthumb.gxdlabs.com/

    The interface to GdThumb is slightly different, for example:

    private function _createThumbnail($sourcePath, $destPath, $w, $h, $q = 100) {
    $thumb = new PhpThumb_GdThumb($sourcePath);
    $thumb->resize($w, $h);
    $thumb->setOptions(array(‘jpegQuality’ => 80));
    $thumb->save($destPath, ‘JPG’);
    }

    Like someone suggested, I thumbnail in the model. I put the PHP Thumb classes in library/PhpThumb and renamed the classes for autoloading as you can see in the example above.

  7. Hi,

    I’m trying to put all stuff in library/PhpThumb directory, but I can’t load clases… I have changed classes names to:

    PhpThumb_GdThumb (extends PhpThumb_ThumbBase)
    PhpThumb_PhpThumb
    PhpThumb_ThumbBase

    plugin directory to: ThumbPlugins

    plugin name to:
    PhpThumb_ThumbPlugins_GdReflectionLib

    Also I’ve changed:
    all file names from *.inc.php to *.php and the THUMBLIB_PLUGIN_PATH to /ThumbPlugins/

    require_once option from:
    require_once THUMBLIB_BASE_PATH . ‘/*.php’; (for example: ThumBase.php)
    to:
    require_once THUMBLIB_BASE_PATH . ‘*.php’;

    Can anybody help me? It would be very nice, if anybody will tell me a solution (and maybe if possible would send files by email..)

  8. I just copied all files to /library/ZendX, renamed the base class to PhpThumbFactory.php and did a require once in my thumbnail view helper require_once ‘/ZendX/PhpThumb/PhpThumbFactory.php’;

    Then I do $thumb = ZendX_PhpThumbFactory::create($image);

    works pretty good for me…

  9. Hi, i have tried but it giving me error like this…
    Fatal error: Call to private Model_DbTable_Thumbnail::__construct() from context ‘Users_AccountController’ in C:Program FilesApache Software FoundationApache2.2htdocsshaileshlyricsapplicationmodulesuserscontrollersAccountController.php on line 427

  10. @Shailesh

    Hi Brother,
    I m not sure, but it seems you made a function private and trying to call it from another Controller.
    If this is the reason and u need to use a function from outside of its class, you should make it public.

  11. I would like to recommend class upload from verot.net – a really powerful upload class that can generate thumbs, do great gd things etc (even from local files, no need to be uploaded files). I am using it successfully for a number of apps over the years (no ZF though).

Leave a Reply to Tobi Cancel reply

Your email address will not be published. Required fields are marked *