【问题标题】:Imagick: setting the gravity on a Imagick itemImagick:设置 Imagick 项目的重力
【发布时间】:2011-08-14 19:30:53
【问题描述】:

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

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

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

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

任何想法如何设置现有图像而不是绘图对象的重力?

谢谢!

【问题讨论】:

    标签: php imagemagick imagick


    【解决方案1】:

    在您的情况下,setGravity 方法应应用于$im 对象。但无论如何,重力似乎只影响 ImagickDraw 对象,插入 drawImage,并且无法像使用 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);
    

    【讨论】:

    • 我完全忘记了我发布了这个,但我还是找到了一个解决方案,就像你建议的那样使用 exec。虽然 Imagick 有一些出色的功能,但文档很糟糕,而且似乎缺少一些更基本的功能。
    猜你喜欢
    • 2019-05-13
    • 2011-07-14
    • 1970-01-01
    • 2015-04-11
    • 2013-03-27
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多