Question

I want to make the mixed image with fade effect from two images.

Here is my PHP code.

$img1 = new Imagick();
$img1->readImage("images/1.jpg");

$img2 = new Imagick();
$img2->setOption('compose:args', '50');
$img2->readImage("images/2.jpg");

$img1->compositeImage($img2, imagick::COMPOSITE_DISSOLVE, 0, 0);
$img1->writeImage("images/3.jpg");

This code works correctly on my Mac OS.

But it doesn't work on remote server (Cent OS 6.5), the result image is same with 2.jpg.

I tried with COMPOSITE_BLEND constant, it works well.

And I tried the command in terminal.

# composite -dissolve 50 2.jpg 1.jpg 3.jpg

It also works well.

Why doesn't COMPOSITE_DISSOLVE constant work in PHP?

Please help me.

Was it helpful?

Solution

You really ought to just figure out what the difference is between your dev environment and your server. It's very likely to be the case that the version of ImageMagick on the dev server is very out of date, unless you are compiling it yourself, as the Centos packages management system seems to take a long time to get new releases.

However....the way you are blending the images is not fantastic. Although it may work for you, it requires the slightly poorly defined $img2->setOption('compose:args', '50'); line.

A much more powerful technique is to control the alpha channel yourself through the COMPOSITE_COPYOPACITY. This gives you full control for how images are blended. e.g.

$img1 = new \Imagick();
$img1->readImage(realpath("../images/Biter_500.jpg"));

$img2 = new \Imagick();
$img2->readImage(realpath("../images/Skyline_400.jpg"));

//Resize images to the same size, to look pretty.
$img1->resizeimage(
 $img2->getImageWidth(), 
 $img2->getImageHeight(),
 \Imagick::FILTER_LANCZOS, 
 1
);

//Create an image that the alpha will be created in.
$opacity = new \Imagick();

if (true) {
    //Create a 50% grey image
    $opacity->newPseudoImage($img1->getImageWidth(), $img1->getImageHeight(), "CANVAS:gray(50%)");
}
else {
    //Create a far more interesting blend.
    //Gradients are created top to bottom, so we need to rotate the image  
    $opacity->newPseudoImage($img1->getImageHeight(), $img1->getImageWidth(), "gradient:gray(10%)-gray(90%)");
    $opacity->rotateimage('black', 90);
}

$img2->compositeImage($opacity, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$img1->compositeImage($img2, \Imagick::COMPOSITE_ATOP, 0, 0);

header("Content-Type: image/jpg");
echo $img1->getImageBlob();

The first way of creating the opacity image with "CANVAS:gray(50%)" does what your existing image code does. The second way of blending creates a image that is blended from 10% to 90% across the width of the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top