我在Imagick中设置图像的重力时遇到了一些真正的困难。
我已经成功地设置了ImaickDraw对象的重力,但在Imagick对象中设置失败。
下面是我目前使用的基本代码。我刚刚使用了与ImagickDraw相同的方法,但显然它不起作用。
$rating = new Imagick("ratings/" . $rating . ".png");
$rating->setGravity (Imagick::GRAVITY_SOUTH);
$im->compositeImage($rating, imagick::COMPOSITE_OVER, 20, 20); 你有什么想法来设置现有图像而不是绘图对象的重力吗?
谢谢!
发布于 2011-07-31 18:07:56
在本例中,应该对$im对象应用setGravity方法。但不管怎样,重力看起来只影响使用drawImage插入的ImagickDraw对象,并且没有办法像使用ImageMagick命令那样将图像放入绘图中。
所以有两种方法可以做到这一点:
第一个。如果您的主机允许函数shell_exec或exec,您可以运行如下命令。
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);https://stackoverflow.com/questions/5820754
复制相似问题