Varien_Image: The Heart of Magento’s Image Manipulation.

Image Manipulation in Magento

Most of you may have experience with Mage_Catalog_Helper_Image class. This class lets you manipulate Magento catalog images in various ways. The most common one is resizing the image and adding watermark. Do you know the majority of the function for this class is a proxy to Varien_Image class. Varien_Image class is also a proxy class that calls the appropriate adapter class. The adapter classes does the heavy work for image manipulation. The default adapter class is Varien_Image_Adapter_Gd2, it interfaces with PHP GD2 to change the images.

Related:[Mini Tutorial] Add Children Categories as Top Navigation

What’s Unique about Varien_Image?

Image Manipulation in Magento

You may ask what the difference is between using Varien_Image and Mage_Catalog_Helper_Image. You should know that there are significant differences between them. The major difference is that Mage_Catalog_Helper_Image has additional logic to handle catalog image cache. It also limits its use to catalog and product images. If you want to manipulate an image that is not from catalog, you would have to use Varien_Image. Varien_Image also doesn’t cache by default. You would have to implement your own caching logic.

The use of each Function

First batch is a set of setting functions. You start with:

$image = new Varien_Image(‘path_to_image.png’);

Here are the resize settings. Pretty self-explanatory, it controls whether to keep the width and height ratio.

$image->keepAspectRatio(true|false);

This one decides how the dimension is calculated if either the width or height value is missing. If it’s true, it copies the defined value, which makes the picture square. Otherwise it becomes calculated based on a ratio of image’s original height and width.

$image->keepFrame (true|false);

This function defines whether the resize dimension can be larger than original.

$image->constrainOnly(true|false);

This function decides whether the background would fill with black or leave transparent for PNG files

$image->keepTransparency(true|false);

Most of you have used this function in catalog image help. It’s also pretty self-explanatory.

$image->resize(width,height)

There is another manipulation function in Varian_Image, it’s the crop function. It takes four integer inputs and crop from the edge. You could combine it with resize to generate image with special needs. For example, you can use crop and resize a regular image into a long width banner.

$image->crop(top, left, right, bottom).

Another self-explanatory function

$image->rotation(angle)

This function is for setting quality of compress image, such as JPEG.

$image->setQuality(integer)

There are also watermark functions which you can utilize to add watermarks.

Cache Mechanism

As I mention before this library does not contain any cache mechanism, so you would have to implement it yourself. The following is the automated one. It simply checks the file modified time, if current time is newer than specified time, it will regenerate the image.

<?php
$io = new Varien_Io_File();

if($io->fileExists(“path_to_cache_image”))
{
   if(now() >  filectime(“path_to_cache_image”)+3600) {
      //Regenerating the image cache code goes here.
   }
   else
   {
      return “URL to cache image”;
   }
}

Resize with Varien_Image

Another way is hooking up with clean_media_cache_after and clean_catalog_images_cache_after observer to clean the cache image directory. If there is enough demand, perhaps I can get in more detail in another post about how to do this. For a documentation of the class click here.

Related: [Mini Tutorial] How to Add Basic Input Fields to Magento Admin Panel