Set an offset from gravity centre

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

Set an offset from gravity centre

Post by hivens »

Hi,

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 geometry.

Here is the code:

Code: Select all

<?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:

Code: Select all

$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.

Any help would be appreciated.

EDIT - I'm coming to the conclusion that this option exists in the command line interface but not in the PHP API?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set an offset from gravity centre

Post by fmw42 »

There is no setGeometry. The geometry offsets are done in compositeImage. I am not sure which image the gravity is set to. It would be $image or $gradient. It is not $draw.
Post Reply