开发者

Perl Image::Magick Method to create a drop shadow (non command line)

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@email.org';
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/ima开发者_StackOverflow中文版ges/someimage.jpg';
        my $contact='email@email.org';
        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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜