Subtracting one image from another

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
paulswansea

Subtracting one image from another

Post by paulswansea »

I'm trying to generate an image where the top part is to be transparent, just leaving the red to the right and to the bottom as solid colour. below is the code, i've set the composite type of the cut-out area to COMPOSITE_LIGHTEN as to show kind of what the end result should look like (apart from the fact the cut-out area isn't transparent) but when i try to use COMPOSITE_SUBTRACT, the whole thing turns cyan. can someone please tell me how to cut out the rounded rectangle from the red background successfully!?

Code: Select all

<?php

	//red background
	$im = array();
	$im[0] = new Imagick();
	$im[0]->newImage(780,30,new ImagickPixel('red'));
	
	//button shading
	$im[1] = new Imagick();
	$im[1]->newPseudoImage(700, 22, 'gradient:#AAAAAA00-#00000060');
	$im[1]->roundCorners(3,3);

	//area to subtract from red background
	$im[2] = new ImagickDraw();
	$im[2]->setFillColor( new ImagickPixel('white') );
	$im[2]->roundRectangle(0,0,698,20,6,6);
	
	$im[3] = new Imagick();
	$im[3]->newImage(705, 25, new ImagickPixel('transparent'));
	$im[3]->drawImage($im[2]);

	// shadow to place just under subtracted area
	$im[4] = clone($im[3]);
	$im[4]->setImageBackgroundColor('black');
 	$im[4]->shadowImage(50,1.5,2,2);

	$canvas = new Imagick();
	$canvas->newImage(705,26, new ImagickPixel('transparent'));

	$canvas->compositeImage($im[0],Imagick::COMPOSITE_MULTIPLY, 0, 0); // background
	$canvas->compositeImage($im[4],Imagick::COMPOSITE_MULTIPLY, 0, 0); // shadow
	$canvas->compositeImage($im[3],Imagick::COMPOSITE_LIGHTEN, 0, 0, Imagick::CHANNEL_ALL); // cut out
	$canvas->compositeImage($im[1],Imagick::COMPOSITE_MULTIPLY, 0, 0); // button shading
	$canvas->setImageColorspace(imagick::COLORSPACE_RGB);
	$canvas->setImageFormat('png');
	$canvas->setCompressionQuality(90);
	header('Content-type: image/' . $canvas->getImageFormat());
	echo $canvas;
	exit();

?>
Post Reply