Imagick: setting the gravity on a Imagick item

こ雲淡風輕ζ 提交于 2019-11-30 09:24:52

问题


I'm having some real difficulties setting the gravity of an image in Imagick.

I've managed to set the gravity of an ImaickDraw object but I've not been successful setting it in a Imagick object.

Below is the basic code I'm using that the moment. I've just used the same as for ImagickDraw but obviously it isn't working.

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

Any ideas how to set the gravity for an exisiting image rather than a draw object?

Thanks!


回答1:


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);


来源:https://stackoverflow.com/questions/5820754/imagick-setting-the-gravity-on-a-imagick-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!