Perl grabbing an external image

Discussion in 'Perl Misc' started by kjhjhjhjadsasda@urbanhabit.com, Dec 15, 2005.

  1. Guest

    Hi

    Im trying to grab an external image (through url) and save it locally
    on the server. Im using image::grab like so:


    use Image::Grab;
    $pic = new Image::Grab;
    $pic->url('http://groups.google.com/groups/img/groups_medium.gif');
    $pic->grab;

    open(IMAGE, ">$serverpath");
    binmode IMAGE;
    print IMAGE $pic->image;
    close IMAGE;

    This saves a file on the $serverpath location but it keeps being
    nothing eg 0k. I remember Ive ran into this previously some time.

    Any suggestions??..

    Thanks!
    M
     
    , Dec 15, 2005
    #1
    1. Advertising

  2. John Bokma Guest

    wrote:

    > Hi
    >
    > Im trying to grab an external image (through url) and save it locally
    > on the server. Im using image::grab like so:
    >
    >
    > use Image::Grab;


    use strict;
    use warnings;


    > $pic = new Image::Grab;
    > $pic->url('http://groups.google.com/groups/img/groups_medium.gif');
    > $pic->grab;
    >
    > open(IMAGE, ">$serverpath");


    check if this succeeds

    > Any suggestions??..


    See above.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Dec 16, 2005
    #2
    1. Advertising

  3. Guest

    John Bokma wrote:
    > use strict;
    > use warnings;
    > ...
    > check if this succeeds


    FWIW, I've never used Image::Grab, but I did give a quick try to the
    OP's code (but within the ordinary BP guidelines as John suggested).
    The $pic object is never populated with data by the grab method. I get
    a warning on the print (because it's printing null), but otherwise the
    code does exactly what the OP says (ie, not much).

    The OP's code is pretty much straight out of the perldocs for the
    module (which are crap, BTW - many examples contain syntax or usage
    errors).
     
    , Dec 16, 2005
    #3
  4. Sisyphus Guest

    <>
    >
    > The OP's code is pretty much straight out of the perldocs for the
    > module (which are crap, BTW - many examples contain syntax or usage
    > errors).
    >


    In that case, I suggest (untested) :

    use strict;
    use warnings;
    use LWP::Simple;
    my $url ='http://groups.google.com/groups/img/groups_medium.gif';

    my $content = get($url);
    open(IMAGE, ">filename.gif") or die "$!";
    binmode IMAGE;
    print IMAGE $content;
    close IMAGE or die "$!";

    __END__

    Cheers,
    Rob
     
    Sisyphus, Dec 16, 2005
    #4
  5. John Bokma Guest

    "Sisyphus" <> wrote:

    >
    > <>
    >>
    >> The OP's code is pretty much straight out of the perldocs for the
    >> module (which are crap, BTW - many examples contain syntax or usage
    >> errors).
    >>

    >
    > In that case, I suggest (untested) :
    >
    > use strict;
    > use warnings;
    > use LWP::Simple;
    > my $url ='http://groups.google.com/groups/img/groups_medium.gif';
    >
    > my $content = get($url);
    > open(IMAGE, ">filename.gif") or die "$!";
    > binmode IMAGE;
    > print IMAGE $content;
    > close IMAGE or die "$!";


    If you don't use LWP::Simple, you can simply (ha ha) drop the file stuff:

    use strict;
    use warnings;

    use LWP::UserAgent;

    my $url = 'http://groups.google.com/groups/img/groups_medium.gif';

    my $response = LWP::UserAgent->new->get(

    $url,
    ':content_file' => 'filename.gif'
    );

    $response->is_success or
    die "Download failed: ", $response->status_line, "\n";

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Dec 16, 2005
    #5
  6. <> wrote:


    > an external image



    What would an "internal image" be then?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 16, 2005
    #6
  7. John Bokma Guest

    Tad McClellan <> wrote:

    > <> wrote:
    >
    >
    >> an external image

    >
    > What would an "internal image" be then?


    The thing after the successfull grabbing :-D

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Dec 16, 2005
    #7
  8. Brian Wakem Guest

    wrote:
    > Hi
    >
    > Im trying to grab an external image (through url) and save it locally
    > on the server. Im using image::grab like so:
    >
    >
    > use Image::Grab;
    > $pic = new Image::Grab;
    > $pic->url('http://groups.google.com/groups/img/groups_medium.gif');
    > $pic->grab;
    >
    > open(IMAGE, ">$serverpath");
    > binmode IMAGE;
    > print IMAGE $pic->image;
    > close IMAGE;
    >
    > This saves a file on the $serverpath location but it keeps being
    > nothing eg 0k. I remember Ive ran into this previously some time.
    >
    > Any suggestions??..




    I don't know the module so I'll assume your code is ok.

    The obvious problem to me is that google block certain useragents, I
    don't know what useragent the module declares itself to be, but it's
    probably blocked, as is LWP::Simple (libwww-perl/x.xxx).

    You'll need to set the useragent to something a browser would use.


    $ perl -MLWP::Simple -e 'print length
    get("http://groups.google.com/groups/img/groups_medium.gif")'

    0

    $ perl -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new( agent =>
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; CS.v0.2; .NET CLR
    1.0.3705)" );my
    $res=$ua->get("http://groups.google.com/groups/img/groups_medium.gif");print
    length $res->content;'

    5183




    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Dec 16, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo

    Re: Grabbing All Cookies

    S. Justin Gengo, Jul 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    S. Justin Gengo
    Jul 29, 2003
  2. Scott Allen
    Replies:
    8
    Views:
    10,949
    Scott Allen
    May 2, 2004
  3. deadpickle

    Grabbing GPS data using perl

    deadpickle, Mar 7, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    91
    deadpickle
    Mar 7, 2008
  4. Mellow Crow
    Replies:
    6
    Views:
    420
    Richard Cornford
    Nov 4, 2005
  5. jobs

    grabbing clipboard image

    jobs, May 15, 2007, in forum: Javascript
    Replies:
    0
    Views:
    77
Loading...

Share This Page