0

What is the Imagick equivalent of following Imagemagick command?

convert i.jpg -set colorspace RGB ( -clone 0 -fill black -colorize 100% ) ( -clone 0 colorspace gray -negate ) -compose blend -define compose:args=70,30 -composite o.jpg

I did the following Imagick commands, but it doesnt seem to be the same

$img = new Imagick("i.jpg");
$img->setImageColorspace(Imagick::COLORSPACE_RGB);
$clone1 = $img->clone();
$clone1->colorizeImage('black', 1.0);
$clone2 = $img->clone();
$clone2->setImageColorspace(Imagick::COLORSPACE_GRAY);
$clone2->negateImage(0);
$img->setOption('compose:args', '70x30');
$img->compositeImage($clone1, Imagick::COMPOSITE_BLEND, 0, 0);
$img->compositeImage($clone2, Imagick::COMPOSITE_BLEND, 0, 0);
$img->writeImage("o.jpg");

Where have I made mistakes?

1 Answers1

0

For the most part, what you have is correct. Two minor issues will need to be addressed to match the CLI result.

First

Move the setOption line before reading any images in the Imagick object.

$img = new Imagick();
$img->setOption('compose:args', '70x30');
$img->readImage("i.jpg");
// ...

Secound

Colorizing the black color to 100% usually results in a solid black image. For whatever reason, there's no effect with MagickColorizeImage method. There's some work-arounds using ImagickDraw & background-color assignment listed within the comments on PHP.net's documentation. I'd recommend revisiting your first $clone1 image.

Community
  • 1
  • 1
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thanks for the help! Why do we need to perform setOption before reading the image? – user1545997 Jan 21 '14 at 02:17
  • I imagine this behavior may be related to PHP's copy-in-memory architecture, as opposed to the traditional pointer, reference, & allocations that you would expected in ImageMagick's (or any) standard library. In the end, it's a small little gotcha that exists in the user-space, not in the library or the API. – emcconville Jan 21 '14 at 02:43
  • ok. Thanks! I have another question related to imagick at http://stackoverflow.com/questions/21248421/imagick-equivalent-of-this-imagemagick-command Can u kindly answer that too? – user1545997 Jan 21 '14 at 03:16
  • @emcconville can you please check this question? https://stackoverflow.com/questions/62777570/how-to-use-auto-level-level-colors-command-argument-into-php-imagemagick – Keyur Shah Jul 21 '20 at 14:13