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);