0

I have a simple convert's imagemagicks command-line which I want to mimic with PHP's Imagick class:

convert \( "image1.jpg" \) \
        \( -size 1x256 gradient:white-black \
           -rotate 90 \
           -fx "floor(u*10+0.5)/10" \
        \) \
        "temp1.jpg"`

That line outputs 2 images: temp1-0.jpg which is just a copy of image1.jpg and temp1-1.jpg which is this one: temp1 As you can see it's a simple gradient image with an effect that makes the gradient not that smooth. And that works perfectly with the command-line imagemagick.

Although when I try to mimic that line with PHP, it does everything correct but the -fx part. The code is the following:

$canvas = new Imagick();
$canvas->newImage(1, 256, "white", "jpg");

$gradient = new Imagick();
$gradient->newPseudoImage(1, 256, "gradient:white-black");
$canvas->compositeImage( $gradient, imagick::COMPOSITE_OVER, 0, 0 );
$canvas->rotateImage(new ImagickPixel(), 90);
$canvas->fxImage("floor(s*10+0.5)/10");

header( "Content-Type: image/jpg" );
echo $canvas;

Like I said, it doesn't reproduce the fx part but rather a normal white-to-black gradient.

I'm new to Imagemagick but I've tried to apply a much simpler effect with that function u+10 but still the effect won't be seen. Am I missing something to make fxImage() work properly? Is there a workaround for an effect like that?

emcconville
  • 23,800
  • 4
  • 50
  • 66
Thomas
  • 222
  • 2
  • 14
  • I haven't used that particular function from imagick, but I wanted to note that if you really cannot find any way to do it through the PHP imagick extension, you could just call the command line version you came up with in PHP, using exec. http://us3.php.net/function.exec – Kai May 14 '14 at 15:40
  • Oh, forgot to say that I'm working on a reselling server that does not allow exec(), therefore the Imagick class. – Thomas May 14 '14 at 15:48

1 Answers1

1

It looks like it's a bug in the Imagick library - The image is actually available, but as the return value of the function.

$canvas = new Imagick();
$canvas->newImage(1, 256, "white", "jpg");

$gradient = new Imagick();
$gradient->newPseudoImage(1, 256, "gradient:white-black");
$canvas->compositeImage( $gradient, imagick::COMPOSITE_OVER, 0, 0 );
$canvas->rotateImage(new ImagickPixel(), 90);
$fxImage = $canvas->fxImage("floor(s*10+0.5)/10");

$fxImage->setImageFormat('jpg');
header( "Content-Type: image/jpg" );
echo $fxImage;

Produces an image with a stepped gradient.

I've opened an issue for this on the Imagick github page.

Danack
  • 24,939
  • 16
  • 90
  • 122
  • I remember reading about that behaviour about fxImage() on an old PHP bugtrack page, fortunately at the time I had an alternative to repoduce that effect. Anyway, that's the way fxImage works and I'm accepting it for the answer (as I'm using it this way now). One thing about your exemple: you should echo `$fxImage` (or compose the `$fxImage` over `$canvas`) to show it properly. Thank you. – Thomas May 15 '14 at 11:42