Using PHP ImageMagick with ImageCache Custom actions - Returning image 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-&gt;source;

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

$distort = array( 0.0, 0.0, 0.16, 1.0  ); 
$newImage-&gt;distortImage( Imagick::DISTORTION_BARREL, $distort, TRUE ); 
$newImage-&gt;resizeImage(1600, 1600, Imagick::FILTER_CATROM, 1);
$newImage-&gt;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);
$newImage-&gt;writeImage($thumb);

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