Centre annotate image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hivens
Posts: 8
Joined: 2017-02-24T03:49:36-07:00
Authentication code: 1151

Centre annotate image

Post by hivens »

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:

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;
}
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:

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);
Any help would be really appreciated, I've hit a wall on this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centre annotate image

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

Suggest you use label: or caption: to create a text image with transparent background and then composite it over your background image. Composite respects both gravity and geometry. If you just want the text image, you can do that with any color background. Both label: and caption respect gravity. See

http://www.imagemagick.org/Usage/text/
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/basics/#parenthesis
Post Reply