3

我在 Imagick 中设置图像的重力有一些真正的困难。

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

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

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

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

谢谢!

4

1 回答 1

4

在您的情况下setGravity,方法应该应用于$im对象。但无论如何,它看起来重力只影响 ImagickDraw 对象,插入drawImage,并且没有办法像使用 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);
于 2011-07-31T10:07:56.473 回答