php 开启imagick,PHP-Imagick:在Imagick项目上设置重力

在Imagick中设置图像的重力时,我遇到了一些实际困难.

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

以下是此刻我正在使用的基本代码.我只是使用了与ImagickDraw相同的方法,但是显然它没有用.

$rating = new Imagick("ratings/" . $rating . ".png");

$rating->setGravity (Imagick::GRAVITY_SOUTH);

$im->compositeImage($rating, imagick::COMPOSITE_OVER, 20, 20);

有什么想法如何为现有图像而不是绘图对象设置重力?

谢谢!

解决方法:

在您的情况下,setGravity方法应应用于$im对象.但是无论如何,重力似乎仅会影响到用drawImage插入的ImagickDraw对象,并且无法像使用ImageMagick命令那样将图像放入绘图中.

因此,有两种方法可以做到这一点:

1号如果您的主机允许使用shell_exec或exec函数,则可以运行类似的命令.

convert image.jpg -gravity south -\

draw "image Over 0,0 0,0 watermak.png" \

result.jpg`

2号否则,您可以计算放置在基础图像上的图像的位置,并使用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,imagemagick,php

来源: https://codeday.me/bug/20191208/2091703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值