22 June 2009 ~ 0 Comments

Image Manipulation in PHP

Sometimes there is a need to do simple image manipulation for certain web projects. Take for example a popular site called Flickr. Flickr is an image hosting site that allows online community to store pictures for others to view. And for some cases some uploaders may have big image files and hence a site like Flickr definitely needs some image processing to reduce the actual image into a smaller dimension, hence smaller file size, to allow quick view before actually downloading the actual size.

I’ve been working with Zend Framework quite sometime for web projects. I found it’s quite interesting to find that Zend Framework does not have any class to perform image manipulation even though a proposal for Zend_Image had been submitted to Zend community as a wrapper of existing PHP interfaces: GD and ImageMagick but unfortunately the proposal was not considered.

And for your information, GD which stands for ‘Gif Draw’ is a standard PHP installation, unlike ImageMagick. If you wish to utilize these libraries, you need to have php extensions called php_gd2 and php_imagick respectively installed and enabled on the web server.

The noticeable difference between GD and ImageMagick is that GD simply does not support image format like TIFF (Tagged Image File Format). If TIFF is not your issue then go for GD. And if you wish to make your development easier, you may be interested to use Thumbnail class by our fellow developer. But if you’re looking into ImageMagick I’m going to show you the steps (from another site) on how to install ImageMagick on your Windows web server as Windows installation is a bit tricky since “pecl install imagick” does NOT work properly.

  1. Download and install ImageMagick software from http://www.imagemagick.org/script/binary-releases.php#windows.
  2. Download pecl-5.2-dev.zip (choose the version relevant to your PHP) from http://snaps.php.net/win32/
  3. Copy php_imagick.dll from the archive you’ve downloaded to your PHP extension folder.
  4. Add the following line to php.ini (in the extensions section):
    extension=php_imagick.dll

    or on WampServer click WampServer icon on taskbar, go to PHP > php.ini and add the above entry.

  5. Restart your server.
  6. Try this example script below to verify your installation.
    header('Content-type: image/tif');
    
    $image = new Imagick('example.tif');
    // Create thumbnail for the specified image file.
    $image->thumbnailImage(100, 0);
    
    echo $image;

    The script should display the image thumbnail with a maximum width of 100 pixels.

If you find the above steps are difficult to follow you may want to download php_imagick.dll here and start from step 3. And if you need help with these libraries, check the manual pages for GD and ImageMagick.

Lastly if you find this post helpful, kindly leave your rating below to indicate that this post is really useful and at the same time let others know about this post. And if your wish to let other communities know about this post kindly click the sociable icons below. Thanks!

Leave a Reply

Spam protection by WP Captcha-Free