Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

LWP to download images, how to let LWP decide, wether it is an image or not?

by khippy (Scribe)
on Oct 20, 2001 at 02:50 UTC ( [id://120170]=perlquestion: print w/replies, xml ) Need Help??

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

I am working on a kind of photo album, where you have a browser admin page to put a picture and text in.
There shall be two possibities to define the picture source. One is upload from your computer, this working fine, so far. The other one shall be an URL to get a picture from the internet. I want to use LWP to keep perlish :) and there is one problem:

There is http or ftp download possible, how do I get LWP only to download images and no other mimetypes?

If you want to look to a snippet of the running code, take a look at my scratchpad at my homenode.
As this code is running at linux I could do a system call with file to proove it is an image *after* having downloaded the file. But this is awful, isn't it?

adTHANXvance
--

there are no silly questions
killerhippy
  • Comment on LWP to download images, how to let LWP decide, wether it is an image or not?

Replies are listed 'Best First'.
Re: LWP to download images, how to let LWP decide, wether it is an image or not?
by oneiros (Scribe) on Oct 20, 2001 at 08:17 UTC

    hihi.

    I haven't tested this yet myself, but since HTTP::Response is a subclass of HTTP::Message, you should be able to:

    if ($res->content_type =~ /^image\//) { ...
      :-) oneiros, you won the combat :-)
      Since I wanted to avoid an extension test for all possible image file types your knowledge about the LWP-Module and its subclasses fits perfectly. A quick test after implementing the code resulted fine.
      I have updated my scratchpad, if you want to take a look, feel free to do so (and all others, too). Note, that the code is not cleaned up, yet. It's developing code :-)

      --

      there are no silly questions
      killerhippy
      I have tested that, and it is probably the best way to do it.

      --
      my one true love
Re: LWP to download images, how to let LWP decide, wether it is an image or not?
by Aristotle (Chancellor) on Oct 20, 2001 at 08:12 UTC
    I've made a Photo Gallery sort of site before, so I know the kind of problem you have. Unfortunately even MIME types cannot really be trusted (esp not from browser uploads either). The only proper solution is to consciously restrict your service to certain image formats (say GIF JPEG PNG; and probably also a maximum filesize) and then use a tool or module to verify the file as a valid GIF/JPEG/PNG after fetching it. (I have used Image::Magick for that before.)
      I like the idea of verifying the file format. You could use Image::Size to check for pictures. And maybe you need the filesize for the HTML-img-Tag (width and height).

      Also I would open the OUTFILE right before the PRINT-statement. If the picture is not valid you end in having created a zero-length file.

      Good bye, Uwe
Re: LWP to download images, how to let LWP decide, wether it is an image or not?
by Beatnik (Parson) on Oct 20, 2001 at 03:37 UTC
    What about something like...
    if ($link =~ /(jpe?g|gif|png)$/i) { ...

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      That's a good effort, but it will not work in all cases. Many sites (photoisland, yahoo photos) use a CGI to pull images out of a database. Although these files do not have the correct extension, they still return a valid image MIME type. Plus, I could fool your pattern like this: http://www.perlmonks.org/?jpg. That probably wouldn't do anything bad, other than a broken image, but still.

      --
      IndyZ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://120170]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-24 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found