I have a script which, given the name of an image, sends the corresponding image to the browser. It would be used in HTML like this:
(Blah blah HTML)
<IMG SRC = "http://www.server.com/cgi-bin/image.pl?iwant=5
(blah blah HTML)
And this would then produce whatever image the server things should be produced for the number "5". My current method of doing things is as follows:
#!/usr/bin/perl
use POSIX;
my $path = "/path/to/images";
my $imagereq = param('iwant');
#If you don't give an image, it assumes you
#want image 0, the defaut image.
unless($imagereq) { $imagereq = 0; }
#If the image you requested exists (all are JPEG), then
#we set our path to reflect this
if(-e "$path/$imagereq".".jpg") {
$path = "$path/$imagereq".".jpg";
}
#Otherwise, we give them an special image
else { $path = "$path"."404.jpg"; }
#Open the image and 'print' it, which the browser
#is quite happy to accept and display as an image
open IMAGE, "$path" or die "ACK! $!";
print "Content-type: image/jpeg\n\n";
binmode STDOUT;
while(<IMAGE>) {
print STDOUT $_;
}
Now, this works just fine. However, I would greatly appreciate any suggestions others may have, either in improving the current code or suggesting a whole other approach.
Some points:
- Is tainting going to be an issue here? All images are numeric (i.e. 1.jpg, 11234.jpg, etc), and since the ".jpg" extension is hardcoded, people shouldnt' be able to use this to try to peek at things they shouldn't be able to, right?
- Actually opening an image and printing it like this seems like a rather brain-dead way of going about it. Is there a better way?
- No, I cannot just set the HTML tag to point to a static location and have the image served that way.
- Is there anything else to consider?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.