Round Corners and Border?

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
spooky25

Round Corners and Border?

Post by spooky25 »

Hello,
About a week ago I noticed that ( and it might just be that I'm the only one experiencing this ) while working with a PNG image with transparency, if I set a border ala borderImage, the transparent areas of the PNG would suddenly become the same color as the border color. This leads me to believe that Imagick creates a larger canvas and composites the PNG onto the canvas. I was able to resolve this issue through the use of DSTOUT ( although it took quite a while to figure out ).

Anywho, a day later I decided I wanted to add rounded corners and that's when my whole world fell apart. Trying to round the corners of an image and add a rounded corner border around it while applying the fix to the transparency problem I mentioned above has proven to be one of the most difficult experiences of my life.

I've determined this much, or atleast what I believe to be 'this much' of the solution. I need to draw a roundRectangle the dimensions of the PNG image + the borders (Layer 1) which will serve as the border, then I need to copy that roundedRectangle and scale it to the size of the PNG image (Layer 2), somehow I need to DSTOUT the shape of Layer 2 onto Layer 1, get the PNG image in the shape of Layer 2 (Layer 3), and then DSTIN the Layer 3 onto Layer 1. Whew! That's a mouthful! While I've been unsuccessful in my attempts to perform exactly to the T the process I just described. I had come up with an alternative but the rounded border looks frankly like ass.

Code: Select all

				$canvas_width = ($this->requestImageCurrentWidth() + ($this->requestImageBorder() * 2));
				$canvas_height = ($this->requestImageCurrentHeight() + ($this->requestImageBorder() * 2));

				$canvas = new Imagick();

				$cutout = new Imagick();
				$cutout->newImage($this->requestImageCurrentWidth(), $this->requestImageCurrentHeight(), "#FF0000");

				$canvas_cutout = new ImagickDraw();
				$canvas_cutout->setFillColor($this->requestImageBorderColor());


				if ($this->requestImageRoundCorners() > 0)
				{
					$cutout->roundCorners($this->requestImageRoundCorners(), $this->requestImageRoundCorners());
					$canvas_cutout->roundRectangle(0, 0, $canvas_width, $canvas_height, $this->requestImageRoundCorners(), $this->requestImageRoundCorners());
				}
				else
				{
					$canvas_cutout->rectangle(0, 0, $canvas_width, $canvas_height);
				}

				$canvas_cutout->composite(Imagick::COMPOSITE_DSTOUT, $this->requestImageBorder(), $this->requestImageBorder(), $this->requestImageCurrentWidth(), $this->requestImageCurrentHeight(), $cutout);

				foreach($this->image_resource as $image_key => $image_frame)
				{
					if ($this->requestImageRoundCorners() > 0)
					{
						$image_frame->roundCorners($this->requestImageRoundCorners(), $this->requestImageRoundCorners());
					}

					$canvas->newImage($canvas_width, $canvas_height, "#FF000000");
					$canvas->drawImage($canvas_cutout);
					$canvas->compositeImage($image_frame, Imagick::COMPOSITE_OVER, $this->requestImageBorder(), $this->requestImageBorder());
					$canvas->setImageFormat($this->requestImageCurrentFormat());
					$canvas->setImageDelay($this->image_resource->getImageDelay());
				}

				$this->image_resource = $canvas->clone();
				$canvas_cutout->clear();
				$canvas_cutout->destroy();
				$cutout->clear();
				$cutout->destroy();
				$canvas->clear();
				$canvas->destroy();
The above code pumps out the following image if I specified $this->requestImageRoundCorners() as 12 and $this->requestImageBorder() as 4.

Image

Hence the reason why I decided that I need to copy and scale the initial roundedRectangle so that it fits better.

Anyways, my question is, how can I accomplish a round corners, bordered, transparency unaffected effect?
Post Reply