首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Imagick:在Imagick项目上设置重力

Imagick:在Imagick项目上设置重力
EN

Stack Overflow用户
提问于 2011-04-28 23:06:00
回答 1查看 3.2K关注 0票数 3

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

我已经成功地设置了ImaickDraw对象的重力,但在Imagick对象中设置失败。

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

代码语言:javascript
复制
$rating = new Imagick("ratings/" . $rating . ".png");
$rating->setGravity (Imagick::GRAVITY_SOUTH);
$im->compositeImage($rating, imagick::COMPOSITE_OVER, 20, 20); 

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-31 18:07:56

在本例中,应该对$im对象应用setGravity方法。但不管怎样,重力看起来只影响使用drawImage插入的ImagickDraw对象,并且没有办法像使用ImageMagick命令那样将图像放入绘图中。

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

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

代码语言:javascript
复制
convert image.jpg -gravity south -\
  draw "image Over 0,0 0,0 watermak.png" \
  result.jpg`

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

代码语言:javascript
复制
$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);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5820754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档