Gossamer Forum
Home : General : Perl Programming :

dimensions of a remote gif file

Quote Reply
dimensions of a remote gif file
I have this simple sub which nicely returns the dimensions of a locally stored gif file:

# Get the dimensions of a GIF file
sub gifdim{
my $file = @_;
my $bytes;
open (FILE,$file) || return();
read(FILE, $bytes, 10);
close(FILE);
$bytes =~ s/^GIF8[79]a// || return(); # Check for GIF header
$bytes =~ s/(.)(.)(.)(.)/$2$1$4$3/ || return(); # Swap bytes
unpack("S2",$bytes);
}

Now what I want is to amend the sub to be able to use it for remote files, so instead of passing in a local path/filename, I pass in a URL http://www.somesite.com/images/image.gif.
Essentially the task is to read the first 10 bytes of a remote file, specified by it's URL.
I've searched like crazy for a solution to this, came across the File::Remote module and similar tools that in my opinion overshoot the target like enabling remote file editing etc.
... in case the solution is basic Perl file handling knowledge ; yes I am a self-taught Perl novice just getting started.

Your help is much appreciated!

Jacob
Quote Reply
Re: [alive2learn] dimensions of a remote gif file In reply to
you probably want to use LWP::UserAgent or LWP::Simple, but it's not clear (at least to me) whether you can do partial fetches.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] dimensions of a remote gif file In reply to
Thanks fuzzy logic! Wink

You got me on the right track, and it's now working using the getstore function from LWP::Simple

Crazy I could not find a way to only retrieve first part of file, so I am storing a copy of the full image file locally, which is far from ideal.

If anybody has an idea on how to fetch only the first n bytes of a remote file, then please let me know.

Jacob
Quote Reply
Re: [alive2learn] dimensions of a remote gif file In reply to
Jacob wrote:
"If anybody has an idea on how to fetch only the first n bytes of a remote file, then please let me know."

Do you need to use getstore() to put it in a file? Why not just use get() and treat it as a bytestream? Of course, you'll get more than the first 10 bytes, but at least you haven't got all the file handling overhead.

BTW, thanks for initiating this topic - and labelling it so clearly. I had a need to get the dimensions of a GIF file and a searh in Google took me straight here!

Velojet