Imagick:在Imagick项目中设置重力

3

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

我已经成功设置了ImaickDraw对象的重力,但是在Imagick对象中设置重力时并没有成功。

以下是我目前使用的基本代码。我只是使用与ImagickDraw相同的代码,但显然它并不起作用。

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

有没有想法如何为现有图片设置重力而不是绘图对象?
谢谢!
1个回答

4
在您的情况下,应该将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);

1
我完全忘记了我发布过这个问题,但是我最终找到了解决方案,正如您建议的使用exec。虽然Imagick有一些很好的功能,但文档质量很差,并且某些基本功能似乎缺失。 - eek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接