Imagick: setting gravity in an Imagick object

I am having some real difficulties installing the image gravity in Imagick.

I was able to set the severity of the ImaickDraw object, but I was not able to set it in the Imagick object.

Below is the base code I'm currently using. I just used the same as for ImagickDraw, but obviously it does not work.

$rating = new Imagick("ratings/" . $rating . ".png");
$rating->setGravity (Imagick::GRAVITY_SOUTH);
$im->compositeImage($rating, imagick::COMPOSITE_OVER, 20, 20); 

Any ideas on how to set gravity for an existing image and not a draw object?

Thank!

+3
source share
1 answer

setGravity $im. , ImagickDraw, drawImage, , ImageMagick.

, :

. shell_exec exec, .

convert image.jpg -gravity south -\
  draw "image Over 0,0 0,0 watermak.png" \
  result.jpg`

. , , compositeImage

$imageHight = $im->getImageHeight();
$imageWith = $im->getImageWidth();

// Scale the sprite if needed.
// Here I scale it to have a 1/2 of base image width
$rating->scaleImage($imageWith / 2, 0);

$spriteWidth = $rating->getImageWidth();
$spriteHeight = $rating->getImageHeight();

// Calculate coordinates of top left corner of the sprite 
// inside of the image
$left = ($imageWidth - $spriteWidth)/2; // do not bother to round() values, IM will do that for you
$top = $imageHeight - $spriteHeight;

// If you need bottom offset to be, say, 1/6 of base image height,
// then decrease $top by it. I recommend to avoid absolute values here
$top -= $imageHeight / 6;

$im->compositeImages($rating, imagick::COMPOSITE_OVER, $left, $top);
+3

All Articles