Centre annotate image
Posted: 2017-02-24T03:56:07-07:00
Hi!
I'm new to imagemagick but managed to get the code below working. However, I've got a bit stuck with centering the text on my image. In an ideal world I want the following:
1. Text vertically and horizontally centre aligned
2. Centre aligned no matter what the font/text size
3. Ideally be able to run a second command to offset it from centre (i.e if there is an icon to be included, I can offset it to the right to make space.)
This is my current code:
The ideas I had for this are to either use gravity, but I couldn't get that working, or finally this example code I found as a standing point which talks about putting one image in the centre of another:
Any help would be really appreciated, I've hit a wall on this.
I'm new to imagemagick but managed to get the code below working. However, I've got a bit stuck with centering the text on my image. In an ideal world I want the following:
1. Text vertically and horizontally centre aligned
2. Centre aligned no matter what the font/text size
3. Ideally be able to run a second command to offset it from centre (i.e if there is an icon to be included, I can offset it to the right to make space.)
This is my current code:
Code: Select all
<?php
function process($inputdata)
{
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
/* New image */
$image->newImage(400, 300, $pixel);
/* Black text */
$draw->setFillColor('black');
/* Font properties */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );
/* Create text */
$image->annotateImage($draw, 10, 45, 0, $inputdata);
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $image;
return;
}
Code: Select all
$imageHight = $image->getImageHeight();
$imageWith = $image->getImageWidth();
$spriteWidth = $rating->getImageWidth();
$spriteHeight = $rating->getImageHeight();
$left = ($imageWidth - $spriteWidth)/2; // do not bother to round() values, IM will do that for you
$top = $imageHeight - $spriteHeight;
$top -= $imageHeight / 6;
$im->compositeImages($rating, imagick::COMPOSITE_OVER, $left, $top);