Perl Image::Magick 创建阴影的方法(非命令行)

发布于 2024-10-14 15:26:52 字数 3393 浏览 4 评论 0原文

我发现这个 PHP 版本似乎是我需要的结果。

    <?php

try
{

    /*** a new Imagick object ***/
    $im = new Imagick('images/spork.jpg');

    /*** set the image format to png ***/
    $im->setImageFormat('png');

    /*** an object for the drop shadow ***/
    $shadow = $im->clone();

    /*** an object for the drop shadow ***/
    $drop_shadow = $im->clone();

    /*** set shadow color to black ***/
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );

    /*** Create the shadow ***/
    $drop_shadow->shadowImage( 80, 3, 5, 5 );

    /*** stick them together ***/
    $drop_shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );

    /*** write image to disk ***/
    $drop_shadow->writeImage( '/tmp/dropshadow.png' );

    echo 'Wrote Image';
}
catch(Exception $e)
{
    echo $e->getMessasge();
}
?>

(试图发布图片,不让我。) 示例如下: 带阴影的 spork

现在,我已经取得了我需要在 Perl 中使用它的结果(使用另一张图像):

#!/usr/bin/perl -w
use strict;
my imageurl='http://nonprofit.org/images/someimage.jpg';
my $contact='[email protected]';
system("montage $imageurl -geometry 476x356  -background '#F7F7F7' -quality 90 -fill '#ffffff' -shadow \ -stroke '#000C' -strokewidth 2 -gravity SouthWest -font Candice -pointsize 14  -annotate +2+1 '$contact' \ -stroke none -fill white -gravity SouthWest -font Candice -pointsize 14  -annotate +2+2 '$contact' \ -gravity center $new");

system("montage $new -geometry 480x360  -background '#F7F7F7' -quality 90 -fill '#F7F7F7' $new");

这给了我一个很好的阴影长宽比(编辑)图像,以 480x360 框/画布为中心,与页面 bgcolor f7f7f7 匹配。

现在,我希望不使用系统方法来做到这一点。

所以,我尝试了这个:

        #!/usr/bin/perl -w
        use Image::Resize;
        use Image::Magick;
        use strict;
        my imageurl='http://nonprofit.org/images/someimage.jpg';
        my $contact='[email protected]';
        my $ibig = Image::Magick->new;
        $ibig->Read("$imageurl");
        $ibig->Resize(geometry=>'476x356');
        $ibig->Montage(geometry=>'476x356',
          background=>'#F7F7F7',
          quality=>90,gravity=>'center',
          shadow=>80x4+4+4); 
    #tried shadow=>'true'  and '1' and many other variations.
     $ibig->Annotate(text=>$contact,
          x=>2,y=>1,
          font=>'Candice',
          pointsize=>14,
          stroke=>'#000C',
          strokewidth=>2,
          gravity=>'SouthWest');

    $ibig->Annotate(text=>$contact,
          x=>2,y=>2,
          font=>'Candice',
          pointsize=>14,
          fill=>'#ffffff',
          stroke=>'none',
          gravity=>'SouthWest');

     $ibig->Montage(geometry=>'480x360',
          background=>'#F7F7F7',
          quality=>90,
          fill=>'#F7F7F7');

$ibig->Write("$new");

这不起作用。注释可以工作,但是没有阴影,并且图像通常最终为 479x360。

系统方法工作完美,但是,我真的想学习如何使用我的 Image::Magick 示例来做到这一点。

我花了两天时间研究这个并阅读 man.

当我想不通的时候,我就去 stackoverflow,总能找到解决方案!

提前致谢。

(抱歉格式问题。我尝试清理它。)

I found this PHP version that seems to be the result I need.

    <?php

try
{

    /*** a new Imagick object ***/
    $im = new Imagick('images/spork.jpg');

    /*** set the image format to png ***/
    $im->setImageFormat('png');

    /*** an object for the drop shadow ***/
    $shadow = $im->clone();

    /*** an object for the drop shadow ***/
    $drop_shadow = $im->clone();

    /*** set shadow color to black ***/
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );

    /*** Create the shadow ***/
    $drop_shadow->shadowImage( 80, 3, 5, 5 );

    /*** stick them together ***/
    $drop_shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );

    /*** write image to disk ***/
    $drop_shadow->writeImage( '/tmp/dropshadow.png' );

    echo 'Wrote Image';
}
catch(Exception $e)
{
    echo $e->getMessasge();
}
?>

(Tried to post image, would not let me.)
Example here: spork with drop shadow

Now, I have achieved results I need using this in Perl (with another image):

#!/usr/bin/perl -w
use strict;
my imageurl='http://nonprofit.org/images/someimage.jpg';
my $contact='[email protected]';
system("montage $imageurl -geometry 476x356  -background '#F7F7F7' -quality 90 -fill '#ffffff' -shadow \ -stroke '#000C' -strokewidth 2 -gravity SouthWest -font Candice -pointsize 14  -annotate +2+1 '$contact' \ -stroke none -fill white -gravity SouthWest -font Candice -pointsize 14  -annotate +2+2 '$contact' \ -gravity center $new");

system("montage $new -geometry 480x360  -background '#F7F7F7' -quality 90 -fill '#F7F7F7' $new");

Which gives me a nice drop shadowed aspect ratio(ed) image centered in a 480x360 box/canvas that matches the pages bgcolor f7f7f7.

Now, I desire to do this without using system method.

So, I tried this:

        #!/usr/bin/perl -w
        use Image::Resize;
        use Image::Magick;
        use strict;
        my imageurl='http://nonprofit.org/images/someimage.jpg';
        my $contact='[email protected]';
        my $ibig = Image::Magick->new;
        $ibig->Read("$imageurl");
        $ibig->Resize(geometry=>'476x356');
        $ibig->Montage(geometry=>'476x356',
          background=>'#F7F7F7',
          quality=>90,gravity=>'center',
          shadow=>80x4+4+4); 
    #tried shadow=>'true'  and '1' and many other variations.
     $ibig->Annotate(text=>$contact,
          x=>2,y=>1,
          font=>'Candice',
          pointsize=>14,
          stroke=>'#000C',
          strokewidth=>2,
          gravity=>'SouthWest');

    $ibig->Annotate(text=>$contact,
          x=>2,y=>2,
          font=>'Candice',
          pointsize=>14,
          fill=>'#ffffff',
          stroke=>'none',
          gravity=>'SouthWest');

     $ibig->Montage(geometry=>'480x360',
          background=>'#F7F7F7',
          quality=>90,
          fill=>'#F7F7F7');

$ibig->Write("$new");

Which does not work. The annotate works but, no drop shadow and the image usually ends up 479x360.

The system method works flawlessly but, I really want to learn how to do this with my Image::Magick example.

I spent two days researching this and reading man.

When I can't figure it out, I come to stackoverflow and ALWAYS get a solution!

Thanks in advance.

(Sorry about formatting issues.. I tried to clean it up.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-10-21 15:26:52

Montage() 返回一个新图像。
尝试:

$ibig = $ibig->Montage(...)

不幸的是,文档没有明确提到这一点,但有本页上的一些示例说明了这一点。

Montage() returns a new image.
Try:

$ibig = $ibig->Montage(...)

The documentation doesn't explicitly mention this, unfortunately, but there are some examples on this page which show this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文