Jimmy
Jimmy

Reputation: 12487

ImageMagick offset text from centre

I'm trying to include a conditional offset to the gravity centre of my text on my generated image. I've done some reading and apparently I can use

Here is the code:

<?php

function process($inputdata)
{
$width = 400;
$height = 300;

$textBackground = new ImagickPixel('transparent');
$textColor = new ImagickPixel('#FFF');

$gradient = new Imagick();
//gradient:angle=angle in degrees
$gradient->newPseudoImage($width, $height, 'gradient:#3a7bd5-#3a6073');

$image = new Imagick();
$image->newImage($width, $height, $textBackground);

$gradient->setImageColorspace($image->getImageColorspace());

$draw = new ImagickDraw();
$draw->setFillColor($textColor);
$draw->setFontSize( 25 );
$draw->setGravity(Imagick::GRAVITY_CENTER );
//$draw->setGeometry(Imagick::Geometry +20 +20); --New code that doesn't work

$image->annotateImage($draw, 0, 0, 0, $inputdata); 

$gradient->compositeImage($image, Imagick::COMPOSITE_OVER, 0, 0);
$gradient->setImageFormat('png');

header('Content-type: image/png');
echo $gradient;
return;
}
?>

This is the code I tried to use:

$draw->setGravity(Imagick::GRAVITY_CENTER );
$draw->setGeometry(Imagick::Geometry +20 +20);

Which in theory should offset the text from the gravity center, however when I try it it stops my image generating. I think I have made a mistake because the only example code I can find is for the command line, not for the PHP interface.

Upvotes: 0

Views: 383

Answers (1)

fmw42
fmw42

Reputation: 53081

There is no equivalent to -geometry. You use the offsets in compositeImage to shift the image. But you need to set the gravity first.

Upvotes: 1

Related Questions