Imagick:设置 Imagick 项目的重力

发布于 2024-11-03 14:14:52 字数 393 浏览 5 评论 0原文

我在 Imagick 中设置图像的重力时遇到了一些真正的困难。

我已成功设置 ImaickDraw 对象的重力,但尚未成功在 Imagick 对象中设置它。

下面是我现在使用的基本代码。我刚刚使用了与 ImagickDraw 相同的方法,但显然它不起作用。

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

有什么想法如何设置现有图像而不是绘制对象的重力吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-11-10 14:14:52

在您的情况下, setGravity 方法应该应用于 $im 对象。但无论如何,重力看起来只影响用 drawImage 插入的 ImagickDraw 对象,并且无法像使用 ImageMagick 命令那样将图像放入绘图中。

所以有两种方法可以做到这一点:

第一。如果您的主机允许使用 shell_execexec 函数,您可以运行如下命令。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文