Seeking equivalent Imagick commands

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
TomXampp
Posts: 29
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Seeking equivalent Imagick commands

Post by TomXampp »

I'm using ImageMagick 7.0.8-66 Q16 x64 on Windows 8.1
My PHP is 7.4.1 on XAMPP.

I've been running the following command successfully with exec() in PHP:

Code: Select all

convert _temp_.jpg 
( +clone -background black -shadow 88x3+2+2 ) 
+swap -background none -layers merge 
+repage -background #eeeeee -layers flatten 
+repage -shave 3x3 
( -size 100x100 xc:#eeeeee ) 
+swap -gravity northwest -geometry +5+5 -compose over -composite output.jpg
It takes an image, resizes it to fit in a 100x100 thumbnail, and adds a drop shadow to the image over a neutral #eeeeee background canvas. It works.

I want to rewrite it to use the Imagick PHP extension, but I'm having trouble translating it. Here's how I've translated it (with annotations), which is not working:

Code: Select all

// convert _temp_.jpg 
$im = new imagick();
$im->readImage('_temp_.jpg');

// ( +clone -background black -shadow 88x3+2+2 ) 
$im_clone = clone $im;
$im_clone->setImageBackgroundColor('black');
$im_clone->shadowImage(88, 3, 2, 2);

// +swap -background none -layers merge 
$im->setImageBackgroundColor('none');
$im->addImage($im_clone);
$im->mergeImageLayers(imagick::LAYERMETHOD_MERGE);

// +repage -background #eeeeee -layers flatten 
$im->cropImage(88, 88, +5, +5);
$im->setImagePage(88, 88, 0, 0);
$im->setImageBackgroundColor('#eeeeee');
$im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);

// +repage -shave 3x3 
$im->cropImage(88, 88, +5, +5);
$im->setImagePage(88, 88, 0, 0);
$im->shaveImage(3, 3);

// ( -size 100x100 xc:#eeeeee )
$im_pseudo = new Imagick();
$im_pseudo->newPseudoImage(100, 100, 'xc:#eeeeee');

// +swap -gravity northwest -geometry +5+5 -compose over -composite output.jpg
$im->setImageGravity(imagick::GRAVITY_NORTHWEST);
$im->compositeImage($im_pseudo, Imagick::COMPOSITE_OVER, 5, 5);

$im->writeImage('output.jpg');
What am I missing?

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Seeking equivalent Imagick commands

Post by fmw42 »

I am not an expert in Imagick. But I suspect it is the +swap that is not being done. That means that your -layers merge is putting the wrong image on top. Likewise for the +swap before the composite, which is likely putting your $im_pseudo image over the other image and it should be the other way.

TomXampp
Posts: 29
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: Seeking equivalent Imagick commands

Post by TomXampp »

Thanks for checking it out. It looks like I'm going to have to break the routine into small steps and compare what the command-line version does versus the PHP Imagick version.

To begin, I've cut out the bottom part of the routine and simply left this, up to the first merge:

Code: Select all

convert _temp_.jpg 
( +clone -background black -shadow 88x3+2+2 ) 
+swap -background none -layers merge 
output.jpg
The equivalent code in PHP Imagick (as I've written it, at least) is:

Code: Select all

// convert _temp_.jpg 
$im = new imagick();
$im->readImage('_temp_.jpg');

// ( +clone -background black -shadow 88x3+2+2 ) 
$im_clone = clone $im;
$im_clone->setImageBackgroundColor('black');
$im_clone->shadowImage(88, 3, 2, 2);

// +swap -background none -layers merge 
$im->setImageBackgroundColor('none');
$im->addImage($im_clone);
$im->mergeImageLayers(imagick::LAYERMETHOD_MERGE);

$im->writeImage('output.jpg');
However, the results are different between the two, so right off the bat there's a problem. I thought it would be enough to switch from $im_clone to $im to reproduce the "+swap" of the command line version. Apparently not?

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Seeking equivalent Imagick commands

Post by fmw42 »

You want to merge the $im over the $im_clone. So try

$im_clone->addimage($im)

Then do the merge on $im_clone

TomXampp
Posts: 29
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: Seeking equivalent Imagick commands

Post by TomXampp »

Thanks for that suggestion. When I use the command-line version, at this point in the code a black 100x100 background image (canvas) is created and an 88x88 version of the input image is superimposed in the northwest corner of it. So, that's the goal for the PHP/Imagick routine at this point.

If I do the following:

Code: Select all

// +swap -background none -layers merge 
$im->setImageBackgroundColor('none');
$im_clone->addImage($im);
$im_clone->mergeImageLayers(imagick::LAYERMETHOD_MERGE);

$im_clone->writeImage('output.jpg');
...then a black 100x100 image is created, with nothing superimposed on top of it (the original, input image is absent in any form).

If I do this instead:

Code: Select all

// +swap -background none -layers merge 
$im->setImageBackgroundColor('none');
$im->addImage($im_clone);
$im->mergeImageLayers(imagick::LAYERMETHOD_MERGE);

$im->writeImage('output.jpg');
...then the original, input image is created at 100x100.

So, neither version of the code reproduces what the command-line version produces at this point. Any clue what's missing?

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Seeking equivalent Imagick commands

Post by fmw42 »

Sorry, I am not an expert on Imagick. But your IM version 7.0.8.66 is rather old. Currently at 7.0.9.13. Perhaps you need to upgrade. Otherwise, perhaps some Imagick expert will reply.

It is odd to me that your input is 100x100 and the shadow image is created at 88x88. It should be 112x112 given -shadow 88x3+2+2. I think you should save each image and verify they are being created the same as in the command line.

Post Reply