opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, How to read size of gif/jpg file which is some here on net like this http://xxxxx.xxx/xxxxxx/xxxxx/xxxx.gif

"I really don't live on earth, it's just my reflection"
"open source is not only technology, but it's a world out there - opensourcer"

Replies are listed 'Best First'.
Re: reading file size of web images
by Corion (Patriarch) on Mar 08, 2005 at 09:30 UTC

    If you only want the file size, you want to check the header of the reply that the server sends to you. The LWP::Simple module has a very convenient method, unimaginatively called head, which does Just That:

    use strict; use LWP::Simple; my $url = 'http://xxxxx.xxx/xxxxxx/xxxxx/xxxx.gif'; printf "$url : %s bytes", ((head $url)[1]);
Re: reading file size of web images
by bart (Canon) on Mar 08, 2005 at 09:42 UTC
    If you mean size as in "bytes", try head() from LWP::Simple, and check out the content-length — second value when called in list context.
    use LWP::Simple; local $\ = "\n"; # add newline to print print +(head('http://perlmonks.org/images/monk1sm.gif'))[1];

    If you mean the image dimensions, you'll have to download the file, for example using get() or getstore() from LWP::Simple, and read the dimensions with Image::Size or Image::Info.

    Here I use Image::Info because it doesn't require the image to be stored as a file — just use a scalar reference to the raw image data:

    use LWP::Simple; use Image::Info qw(image_info dim); my $raw = get('http://perlmonks.org/images/monk1sm.gif'); printf "WxH: %dx%d\n", dim(image_info(\$raw));
Re: reading file size of web images
by manav (Scribe) on Mar 08, 2005 at 11:21 UTC
    use Image::Size
    gives plenty of possible combinations in whch you would like to get the size......

    Manav
Re: reading file size of web images
by brian_d_foy (Abbot) on Mar 08, 2005 at 14:25 UTC

    If you want the file size, you can use HTTP::Size, which I wrote just for that purpose.

    --
    brian d foy <bdfoy@cpan.org>
Re: reading file size of web images
by Hena (Friar) on Mar 08, 2005 at 09:27 UTC
    I would try 'wget --spider link'. If i remember correctly it returns file size as well. Could be wrong so test it first :).
Re: reading file size of web images
by holli (Abbot) on Mar 08, 2005 at 09:24 UTC
    in bytes or pixels?

    Either way you have to download the image to tell.


    holli, /regexed monk/