trouble with rotated ImagickDraw objects

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
flashape

trouble with rotated ImagickDraw objects

Post by flashape »

I am having a little bit of trouble getting rotated ImagickDraw objects to position correctly when calling Imagick->drawImage().

I am drawing a rectangle with some text within it. I first call ImagickDraw->translate(), then call ImagickDraw>rotate, then draw a border, then add the text. After that is complete i call Imagick->drawImage() to add the Draw object to an existing Imagick object.

No matter what i do I cannot get the box to align correctly. I have tried doing all the drawing first, then translating, then rotating. I have also tried drawing, rotating, translating, and pretty much every combo of those steps.

In the code below, it looks like the drawing is in the correct position horizontally, but is way too high up vertically, and i think that's about as close I got it. Right now I'm going to draw and draw into a temp image and just composite the temp image onto the existing image, but that seems like a waste of resources. Any help is appreciated.

Here's the code:

Code: Select all

function addTextToPage($textVO){
		
		

		$imagickDraw = new ImagickDraw();
		$imagickPixel = new ImagickPixel();

		//translate the coordinates of where to the place the textfield in the image here
		$imagickDraw->translate($textVO->boundingBox['x'], $textVO->boundingBox['y']);
		
		
		if ( $textVO->rotation < 0 ){
			$rot = 360 - abs($textVO->rotation);
		}else{
			$rot = $textVO->rotation;
		}
		
			
		$imagickDraw->rotate( $rot );
	
		
		// draw text box bg
		$imagickPixel->setColor( 'transparent' );
		$imagickDraw->setFillColor($imagickPixel);
		
		// draw text box stroke
		$imagickPixel->setColor( $textVO->borderColor );
		$imagickDraw->setStrokeColor($imagickPixel);
		$imagickDraw->setStrokeWidth($textVO->borderThickness);
		$imagickDraw->rectangle(0,0, $textVO->width, $textVO->height);
			
			
		// draw text
		$imagickPixel->setColor( '#000000' );
		$imagickDraw->setFillColor($imagickPixel);
		$imagickDraw->setStrokeColor($imagickPixel);
		$imagickDraw->setStrokeWidth(0);
		
		if( empty($textVO->font) ){
			$font = 'Arial';
		}else{
			$font = $textVO->font;
		}
		
	
		
		$imagickDraw->setFont( $font );
		$imagickDraw->setFontSize( $textVO->size );
		$imagickDraw->annotation ( 10, 10, $textVO->text );
		

		$this->layeredImage->drawImage($imagickDraw);
		
	}
Post Reply