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
總結
以上是生活随笔為你收集整理的php 开启imagick,PHP-Imagick:在Imagick项目上设置重力的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 喝崂山啤酒中奖不能兑换崂山同款啤酒?
- 下一篇: 卡尔曼滤波器求速度matlab,卡尔曼滤
