How to Use Imagick to Merge and Mask Images

How to use Imagick to merge and mask images?

So, finally, this should do what you need:

Original image:

http://i.stack.imgur.com/b7seR.png

Opacity mask:

Sample Image

Overlay:

http://i.stack.imgur.com/3ulkM.png

Output:

Sample Image

The code:

<?php
$base = new Imagick('U0R4F.png');
$mask = new Imagick('mask.png');
$over = new Imagick('3ulkM.png');

// Setting same size for all images
$base->resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1);

// Copy opacity mask
$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);

// Add overlay
$base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);

$base->writeImage('output.png');
header("Content-Type: image/png");

echo $base;
?>

I hope it's right now!
Note: In your example it looks like you downscaled the base image, which I didn't (my goal is just to show how the masking is done).

How to use Imagick to merge and mask images?

So, finally, this should do what you need:

Original image:

http://i.stack.imgur.com/b7seR.png

Opacity mask:

Sample Image

Overlay:

http://i.stack.imgur.com/3ulkM.png

Output:

Sample Image

The code:

<?php
$base = new Imagick('U0R4F.png');
$mask = new Imagick('mask.png');
$over = new Imagick('3ulkM.png');

// Setting same size for all images
$base->resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1);

// Copy opacity mask
$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);

// Add overlay
$base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);

$base->writeImage('output.png');
header("Content-Type: image/png");

echo $base;
?>

I hope it's right now!
Note: In your example it looks like you downscaled the base image, which I didn't (my goal is just to show how the masking is done).

Imagemagick to merge and mask images

This should give you a result very much like your example...

convert mask.png image.png -gravity north -composite \
mask.png -compose copyopacity -composite result.png

You can adjust the positioning of the overlay by including something like "-geometry -10+0" before the first composite.

To use this command in Windows you'll need to change that continued line backslash "\" to a caret "^".

Imagick: compose with mask

After struggling with this I finally found a way how to do it properly with PHP-Imagick.

// merge x-displacement and y-displacement into one displacement-map
$displaceMask = new Imagick();
$displaceMask->addImage($a3);
$displaceMask->addImage($a4);
$displaceMask->addImage($a4);
$displaceMask->flattenImages();
$displaceMask = $displaceMask->combineImages(Imagick::CHANNEL_ALL);

$displaceMask->setImageArtifact('compose:args', '312x26.6776');
$a1->compositeImage($displaceMask, Imagick::COMPOSITE_DISPLACE, 0, 0);

Resources that I used:

  • https://github.com/mkoppanen/imagick/issues/49
  • https://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=26323
  • Merging RGB channels back into a single image with ImageMagick (php)
  • https://imagemagick.org/script/command-line-options.php#combine

How to use mask and combine 2 images into 1 using imageMagick?

If you want your image to be 318px x 235px (to fit in the window of your template), you can resize
using ImageMagick:

convert photo-big.jpg -resize '318x235^' photo-small.jpg 

The ^ insures that the small version will not be any smaller than the given dimensions, although one dimension may be bigger, in order to maintain the aspect ratio.

In order to insert the resized image into your template, you'll need a third image to serve as a mask. The mask is only black and white: the white is where you want the photo to show up in your template, like this:
mask for template

Use the following line of code to create your composite image. The geometry indicates the offset in pixels (59 down and 15 over) to position the photo in the window:

composite -geometry '+15+59' photo-small.jpg template.jpg mask.jpg comp.jpg

Imagick combine images

how to combine images withouw whitespaces between them?

The thumbnail_geometry should contain the width/height and offset (e.g. WxH+L+T).

To eliminate the whitespace, the third parameter would be ...

$stack->montageImage(new ImagickDraw(),
$placing,
$this->resultWidth.'x'.$this->resultHeight.'+0+0',
0,
0);

PHP Imagick clipping mask results in dark halo edges

Taking the commands from fmw42 I managed to come up with the following PHP code that seems to do the same thing, and works;

//$shape is my first image, $reflection is my second image, as Imagick instances

//extract shape alpha channel
$shapeAlpha = clone $shape;
$shapeAlpha->separateimagechannel(\Imagick::CHANNEL_ALPHA);

//extract reflection alpha channel
$reflectionAlpha = clone $reflection;
$reflectionAlpha->separateimagechannel(\Imagick::CHANNEL_ALPHA);

//merge both alpha channels
$shapeAlpha->compositeImage($reflectionAlpha, \Imagick::COMPOSITE_MULTIPLY, 0, 0);
$combinedAlpha = clone $shapeAlpha;

//clip the reflection with the combined alpha channel
$reflection->setImageMatte(false);
$combinedAlpha->setImageMatte(false);
$reflection->compositeImage($combinedAlpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);

//apply the reflection to the shape
$shape->compositeImage($reflection, \Imagick::COMPOSITE_OVER, 0, 0);

//$shape is now the final composite image as intended, no dark halo


Related Topics



Leave a reply



Submit