sfImageTransformPlugin - 0.1.0

symfony plugin for manipulating images using the GD or ImageMagick libraries

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source

sfImageTransformPlugin plugin

The sfImageTransformPlugin allows the easily manipulation of images. The plugin requires either either GD or ImageMagick graphics libraries to be installed on the server.

sfImageTransform works by applying one or more "transforms" to the image. A transform maybe simple like resizing or mirroring or more complex like overlays and pixelizing. Multiple tranforms can be easily applied by chaining the transform calls as seen below. It is also very easy to extend and create your own transforms.

Load an image, resize it to 100 pixels wide (preserving the aspect ratio) and rotate it 90 degrees:

$img = new sfImage('image1.jpg', 'image/jpg');




To install the plugin for a symfony project, the usual process is to use the symfony command line:

symfony plugin-install http://plugins.symfony-project.com/sfImageTransformPlugin

Alternatively, if you don't have PEAR installed, you can download the latest package attached to this plugin's wiki page and extract it under your project's plugins/ directory.

Clear the cache to enable the autoloading to find the new classes:

php symfony cc


Example 1 Chaining

The simplest way to use sfImageTransform is to use method chaining. In an action:

$img = new sfImage('image1.jpg', 'image/jpg');

$response = $this->getResponse();



return sfView::NONE;

Example 2 Standalone

$img = new sfImage('image1.jpg', 'image/jpg', 'ImageMagick');

$scale = new sfImageScaleImageMagick(0.5);

$img = $scale->execute($img);

$img->saveAs('image2.gif', 'image/gif');


You can override the default settings used by redefining the plugin settings in your project


    default_adapter: GD # GD or ImageMagick
    default_image: { mime_type: image/png, filename: Untitled.png }
    font_dir: /usr/share/fonts/truetype/msttcorefonts

Writing your own transforms

sfImageTransformPlugin is designed to be easily extended. To make a new transform you simple create a class that extends the abstract class (sfImageTransformAbstract) and implements the transform method.

Transforms are written specifically for the image library you want to use. The naming convention for a transform is important. The class files should be named sfImage#transform name##image library#.class.php and class names should be sfImage#transform name##image library#

Let's create a GD transform called "Example".


class sfImageExampleGD extends sfImageTransformAbstract
  // Parameters can be passed in the standard way
  public funnction __construct($arg1, $arg2)

  public function execute(sfImage $image)

    // Get the actual image resource  
    $resource = $image->getAdapter()->getHolder();

    // Manipulate image using the GD functions

    // To set a new resource for the image object



Outputting the image using an action

$img = new sfImage('image1.jpg', 'image/jpg');

$img->resize(1000,null)->example($arg1, $arg2);

$response = $this->getResponse();

// Output the right content type


return sfView::NONE;

Included tranforms


  • arc
  • brightness
  • colorize, contrast, crop
  • edgeDetect, ellipse, emboss
  • fill, flip
  • gamma, gaussianBlur, greyscale
  • line
  • mirror
  • negate, noise
  • overlay
  • pixelBlur, pixelize
  • rectangle, resize, rotate
  • scale, scatter, selectiveBlur, sketchy, smooth
  • text, transparency


  • crop
  • fill, flip
  • mirror
  • prettyThumbnail
  • rectangle, resize, rotate
  • scale
  • text, trim