想象一下:在一个Imagick项目上设置重力 [英] Imagick: setting the gravity on a Imagick item

查看:99
本文介绍了想象一下:在一个Imagick项目上设置重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Imagick设置图像的重力时遇到了一些实际困难。

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

我设法设置ImaickDraw对象的重力但我已经没有成功在Imagick对象中设置它。

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

下面是我正在使用的基本代码。我刚刚和ImagickDraw一样使用了它,但显然它不起作用。

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?

谢谢!

推荐答案

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

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。如果您的托管允许函数 shell_exec exec ,您可以运行类似的命令。

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`

第二名。否则,您可以计算放置在基本图像上的图像的位置,并使用 compositeImage

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

这篇关于想象一下:在一个Imagick项目上设置重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆