in reply to 'Printing' an Image to a Browser
#!/usr/bin/perl -w use strict; use CGI qw(:standard); use CGI::Carp; my $path = "/path/to/images"; my $imagereq = param('iwant'); $imagereq ||= 0; # defaults to image 0 my $image_path = "$path/$imagereq.jpg"; # check there are digits only in the request and that the file exists $image_path = "$path/404.jpg" unless $imagereq =~ /^\d+$/ and -e $imag +e_path; open(IMAGE, "<", $image_path) or die "ACK! $!"; print "Content-type: image/jpeg\n\n"; binmode STDOUT; $/ = \8192; # read data in 8kbyte chunks rather than from LF to LF # see perldoc perlvar print while <IMAGE>;
It would probably be cleaner to use File::Spec::Functions to do the path concatenations. Of course, if the files are located inside your DocumentRoot, rather than sending the file you could just send a Location: redirect.
HTH :-)
Update Aug 12, 2002 (including minor code fix): silly twit, I am. Of course, if $image_path is "$path/$imagereq.jpg", passing ?iwant=>0 to the script will not clobber anything, and simply lead to the -e test failing harmlessly. I'm surprised noone called me on it. Using the three-argument open() is generally recommended anyway; simply a matter of defensive coding.Makeshifts last the longest.
|
|---|