Imagemagick command line in PHP - php

I've got the following two commands for imagemagick in the command line:
convert in.png container-mask.png +matte -compose CopyOpacity -composite out.png
composite container.png out.png -compose multiply final.png
Those two commands include 3 files:
in.png: the file that should be masked
container-mask.png: the back/white mask of the areas of container.png where in.png should be visible
container.png image that includes the container for in.png, the one that has been masked in black/white with container-mask.png
Now the question is how to transpose this commands into PHP calls. I've played around quite a bit, but I can't make sense of the API at http://php.net/manual/en/book.imagick.php
Thanks and Bests,
Charly

I've found the answer. Well, that was not too complicated after all:
$original = new Imagick("in.png");
$mask = new Imagick("container-mask.png");
$container = new Imagick("container.png");
$mask->setImageMatte(0);
$original->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$container->compositeImage($original, Imagick::COMPOSITE_MULTIPLY, 0,0);
$container->setImageFormat( "png" );
echo $container;

Related

ImageMagick displacement does not work as expected

I'm using latest ImageMagick with PHP and this is my code:
$diplacementmap = new Imagick('displacement.png');
$android = new Imagick('android.png');
$android->compositeImage($diplacementmap, Imagick::COMPOSITE_DISPLACE, 0, 0);
echo $android;
For some reason, I'm not getting expected results. Why could it be?
Both files separately:
Android - https://i.stack.imgur.com/bYbYX.png
Displacement map - https://i.stack.imgur.com/6g4v9.png
Set your displacement values to 10,10 in ImageMagick or Imagick. When you set the arguments to 0,0, you may be getting some larger default.
For example in command line:
Input:
Displacement map:
convert android.png displacement.png -define compose:args=10,10 -compose displace -composite result10.png
or at 20
convert android.png displacement.png -define compose:args=20,20 -compose displace -composite result20.png

Convert convert syntax to PHP (ImageMagick)

I'm very new to the whole ImageMagick PHP library. I need to port this function to PHP using ImageMagick:
convert staticmap.png -gaussian-blur 10
\( -size 300x600 gradient:'rgba(255,255,255,0.9)'-'rgba(255,255,255,0.1)' -rotate 270 \)
-gravity north -compose over -composite output.png
or something that will give this output:
I can't use shell_exec like I always have because I'm running on Google App Engine and I don't think that function is enabled.
Is there an easier way to get the desired result? I want to blur it, too, but I think I can figure that part out.
EDIT: found a better way to do this on the command line. Hopefully that'll help conversion into PHP?
This is easy as all CLI options map directly to ImagickMagick.
<?php
/* convert */
// staticmap.png
$staticMap = new Imagick('staticmap.png');
// -gaussian-blur 10x0
$staticMap->gaussianBlurImage(10, 0);
// -size 300x600 gradient:'rgba(255,255,255,0.9)'-'rgba(255,255,255,0.1)'
$mask = new Imagick();
$mask->newPseudoImage(300, 600, 'gradient:rgba(255,255,255,0.9)-rgba(255,255,255,0.1)');
// -rotate 270
$mask->rotateImage('black', 270);
// -gravity north
$staticMap->setGravity(Imagick::GRAVITY_NORTH);
// -compose over -composite
$staticMap->compositeImage($mask, Imagick::COMPOSITE_OVER, 0, 0);
// output.png
$staticMap->writeImage('output.png');

Imagick - Composite image mask through PHP

I'm trying to recreate a script that uses the ImageMagick command "convert" to compose an image. But I want to do the same in PHP using Imagick:
convert ./a.png ./b.png ./c.png -virtual-pixel transparent -channel rgba -alpha on -background transparent -define compose:args=300x53.033 -compose displace -composite ./final.png
a.png
b.png
c.png
Result:
Where a.png is a base image, b.png is the overlay and c.png is the gray-scale 'mask'. For achieving this result, I've to execute all manipulations which are supported by the extension, write the image in disk and then execute this command through the exec function, which is a terrible workaround.
The following snippet does not work:
$a = new Imagick('../a.png');
$b = new Imagick('../b.png');
$c = new Imagick('../c.png');
$a->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$a->setImageBackgroundColor(new ImagickPixel('none'));
$a->setOption('compose:args', '300x53.033');
$a->compositeImage($c, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$a->compositeImage($b, Imagick::COMPOSITE_DISPLACE, 0, 0);
Result:
Expected:
I would like to achieve this same result using only Imagick PHP.

What is Imagick equivalent of following Imagemagick command?

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?
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.

Moving from convert command to straight PHP for ImageMagick

I am moving to a new hosting company that will not allow me to exec a convert command for ImageMagick. So I now have to attempt to do it through straight PHP. I have spent quite a bit of time trying to figure it out and every where that I look, people recommend using the convert command like I am. I would appreciate any help or guidance in writing the following commands in straight PHP.
# Applies overlay $filter_image to the original $image
convert $image ( -clone 0 -alpha off $filter_image -compose SoftLight -composite ) -compose SrcIn -composite $output_image
and
# Apply a blur to an image
convert $image -blur 0x$blur_radius $output_image
UPDATE:
I have figured out the syntax and posted it as an answer.
Best of luck Joe; I would recomend changing to a host that will let you use exec.
I have some examples of the imagick functions on my site that you may be able to cobble something together with: http://www.rubblewebs.co.uk/imagick/functions/function.php
I have just noticed I posted the Imagemagick code not the Imagick !
This is as you now know the blur code for Imagick:
bool blurImage ( float $radius , float $sigma [, int $channel ] )
<?php
$im = new Imagick($input);
$im->blurImage( 0, 3 );
$im->writeImage('blurImage.jpg');
$im->destroy();
?>
Might be worth adding an Imagick tag to your post as that is what you want to use?
I finally figured it out on my own. Here is the solution in case anyone else runs into this.
Blur an image...
$imagick = new Imagick($image);
$imagick->blurImage(0,$blur_radius);
$imagick->writeImage($output_image);
Add an overlay to an image...
$imagick = new Imagick($image);
$overlay = new Imagick($filter_image);
$imagick->compositeImage($overlay, imagick::COMPOSITE_SOFTLIGHT, 0, 0);
$imagick->writeImage($output_image);
You can easily combine the two methods as well and blur the image and then add a composite overlay to it.
$imagick = new Imagick($image);
$imagick->blurImage(0,$blur_radius);
$overlay = new Imagick($filter_image);
$imagick->compositeImage($overlay, imagick::COMPOSITE_SOFTLIGHT, 0, 0);
$imagick->writeImage($output_image);

Categories