In your case setGravity
method should be applied to $im
object. But anyways it looks like the gravity affects only ImagickDraw objects, inserted with drawImage
, and there's no way to put an image in a draw like you can do with ImageMagick commands.
So there's two ways to do this:
1st. If your hosting allows functions shell_exec
or exec
, you can run a command like.
convert image.jpg -gravity south -
draw "image Over 0,0 0,0 watermak.png"
result.jpg`
2nd. Otherwise, you can calculate position of the image being placed on the base image and use compositeImage
$imageHight = $im->getImageHeight();
$imageWith = $im->getImageWidth();
// Scale the sprite if needed.
// Here I scale it to have a 1/2 of base image's 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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…