Transparency and compositing images
Posted: 2008-08-27T08:11:20-07:00
I just started using imagemagick, and I'm trying to do something that should be simple. I want to lay a bunch of images on top of eachother, creating one image.
The problem I'm having is:
1) How do I actually combine the images correctly? Which constant / compositing mode should I choose? COMPOSITE_MULTIPLY seems to be the only one that comes close to the correct behavior, but it doesn't allow me to put an image under the other
In my example code below, when I combine "hair_back" and then the base image, the hair ends up on top of the base
I just want it to behave "normally": the way it would behave if you opened any modern image program, took one image and copied+pasted it on top of another
(please don't suggest alternative solutions such as Imagick::combineImages() because I plan to do other things once I get past this hurdle, such as changing the color of an image before it's composited on top of another)
2) How do I get transparency in my output image?. I want to output 8-bit .pngs with transparency (not the alpha channel kind). Currently it has a white background..... which isn't surprising, but, I don't know how to use a transparent "canvas" or change the white to transparency at the end
The problem I'm having is:
1) How do I actually combine the images correctly? Which constant / compositing mode should I choose? COMPOSITE_MULTIPLY seems to be the only one that comes close to the correct behavior, but it doesn't allow me to put an image under the other
In my example code below, when I combine "hair_back" and then the base image, the hair ends up on top of the base
I just want it to behave "normally": the way it would behave if you opened any modern image program, took one image and copied+pasted it on top of another
(please don't suggest alternative solutions such as Imagick::combineImages() because I plan to do other things once I get past this hurdle, such as changing the color of an image before it's composited on top of another)
2) How do I get transparency in my output image?. I want to output 8-bit .pngs with transparency (not the alpha channel kind). Currently it has a white background..... which isn't surprising, but, I don't know how to use a transparent "canvas" or change the white to transparency at the end
Code: Select all
header('Content-type: image/png');
$im_base = new Imagick('buildpieces/body_default.svg');
$im_base->scaleImage(160,0);
$canvas = new Imagick();
$canvas->newImage($im_base->getImageWidth(), $im_base->getImageHeight(), "white");
$im['hair_front'] = new Imagick('buildpieces/hair_front.svg');
$im['hair_back'] = new Imagick('buildpieces/hair_back.svg');
$im['glasses'] = new Imagick('buildpieces/glasses.svg');
foreach ($im as &$image) {
$image->scaleImage(160,0);
}
$canvas->compositeImage($im['hair_back'], imagick::COMPOSITE_MULTIPLY, 0, 0);
$canvas->compositeImage($im_base, imagick::COMPOSITE_MULTIPLY, 0, 0);
$canvas->compositeImage($im['glasses'], imagick::COMPOSITE_MULTIPLY, 0, 0);
$canvas->compositeImage($im['hair_front'], imagick::COMPOSITE_MULTIPLY, 0, 0);
$canvas->setImageFormat('png');
$canvas->setCompressionQuality(90);
echo $canvas;