I'm trying to composite some images using ImageMagick - I know imagemagick via PHP, i'm trying to return the image via PHP but it seems to overwrite the original source file - not ideal as it can apply the filter over and over.

Ideally, I'd like to write the original file with _thumb after it, then call this file via Drupal for display, where it would copy to the appropriate imagecache directory and display from there. Anyone with ImageMagick experience, would be appreciated if you can help.

$location = $image->source;

$newImage = new Imagick($_SERVER['DOCUMENT_ROOT'].'/'.$image->source);

$distort = array( 0.0, 0.0, 0.16, 1.0  ); 
$newImage->distortImage( Imagick::DISTORTION_BARREL, $distort, TRUE ); 
$newImage->resizeImage(1600, 1600, Imagick::FILTER_CATROM, 1);
$newImage->cropImage(1500, 1500,60, 50);

$extension_pos = strrpos($fileName, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($fileName, 0, $extension_pos) . '_thumb' . substr($fileName, $extension_pos);

return $thumb;
  • 2
    Where does this code live?
    – Clive
    Sep 4, 2013 at 15:32
  • 2
    In the 'Custom Actions' for imagecache preset Sep 5, 2013 at 9:35

1 Answer 1


Someone might find this useful as Documentation is very sparse for this (this took me over a day to figure out). The problem was I was writing to a seperate file - Works, but ImageCache will delete it, also defeats the point of using ImageCache.

The solution is as follows, to use $image->ops[] and ImageMagick CMD's. (This is entered in the custom actions of imagecache. Distort image, apply mask, and apply overlay. This is used for creating product images for a Shop with just normal images supplied. I'll probably make a blog post in more depth at some point.

$image->ops[] = "-distort Barrel '0.0 0.0 0.16 1.0'";
$image->ops[] = "-resize 1600x1600";
$image->ops[] = "-crop 1500x1500+60+50";
$image->ops[] = "'sites/default/files/coushin-bg.png' -compose Dst_In  -gravity center -alpha Set -composite -format png -quality 90 ";
$image->ops[] = "'sites/default/files/Blank-Cushion.png' -compose Multiply  -composite -format png -quality 90 ";
return $image;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.