Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Serving images with perl

by fluffyvoidwarrior (Monk)
on Oct 14, 2005 at 07:29 UTC ( #500146=perlquestion: print w/replies, xml ) Need Help??
fluffyvoidwarrior has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'm using apache and cgi. I want to return pages in response to browser requests that display images that are served by a perl script, Something like:-

<img src=" +e=myimage.gif">


<img src="">

Basically I don't want to serve users private data from a common(ish) directory on the apache tree but from secure locations only accessible via my scripts. All your help is highly appreciated.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Serving images with perl
by Fang (Pilgrim) on Oct 14, 2005 at 08:01 UTC

    You have to know two things:

    1. the mime type of the image
    2. the size of the image

    Then it's all a matter of sending the correct HTTP header, and then simply printing the image.

    use CGI qw(:standard); my $mime_type = 'image/jpeg'; my $size = length($data); # Assuming $data contains your image print header( -type => $mime_type, -Content_Length => $size, ); print $data;

    Now, how you get the mime type and the image is up to you, it could be from a database or from a file on disk.

Re: Serving images with perl
by Adrade (Pilgrim) on Oct 14, 2005 at 10:21 UTC
    use CGI; my $img = CGI->new->param('name'); my ($type) = (lc($img) =~ m/([^\.]+)$/s); my %switch = qw/jpg jpeg tif tiff/; $type = $switch{$type} if exists $switch{$type}; print "Content-type: image/$type\n\n"; open(IMG,"</path/to/files/$img"); binmode(IMG); binmode(STDOUT); print while <IMG>; close(IMG);
    My two cents,

    By a scallop's forelocks!

Re: Serving images with perl
by Delusional (Beadle) on Oct 14, 2005 at 10:53 UTC
    You may find the information here usefull. I asked a simular question some time ago.
Re: Serving images with perl
by EvanCarroll (Chaplain) on Oct 14, 2005 at 07:50 UTC
    I'm not sure of your level of perl knowledge, but you might consider checking Mason's dhandler feature which does just what you want.

    But your question being direct would require you to simply parse $ENV{'QUERY_STRING'} in your

    Evan Carroll
Re: Serving images with perl
by cbrandtbuffalo (Deacon) on Oct 14, 2005 at 15:13 UTC
    If you are going to read in the files and serve them through the perl script, you may want to check out this article on slurping in files quickly.
        I worked it out in the end. Its head slappingly simple Thanks everyone Heres the finished script:-
        #!/usr/bin/perl -w =comment This is for customer proofs NOT cam/management proofs Responds to a request for a customer proof image from the internet +. Reads the requested image from disk and serves it back to the brow +ser. Used to serve private images that are not on the apache tree in re +sponse to a browser request to fill an image tag eg <img src=\" +i?load_image=cust__stevecaralancom1129266425_00.gif\" border=\"0\" a +lt=\"\"> =cut use strict; use CGI; #use File::Copy; #use Tie::File; #use Fcntl qw( :DEFAULT :flock ); use steves_general_functions; my $mycgi = new CGI; my $imagedata = ""; my $load_image = $mycgi->param( "load_image" ); $load_image = &gen_wash_input($load_image, "filename"); my $proof_folder = "/caralan_com/system/proofing/customer_proofs/proof +s_out/"; my $imagefile = $proof_folder . $load_image; #Set mime type for returned binary dump if( lc(&gen_get_extension_from_path($imagefile)) eq ".gif"){ print "Content-type: image/gif\n\n"; } if( lc(&gen_get_extension_from_path($imagefile)) eq ".jpg"){ print "Content-type: image/jpeg\n\n"; } if( lc(&gen_get_extension_from_path($imagefile)) eq ".tif"){ print "Content-type: image/tif\n\n"; } #Slurp binary image data from diskfile { local( $/, *IMAGEFILE ) ; open( IMAGEFILE, $imagefile ); $imagedata = <IMAGEFILE>; close IMAGEFILE; } #Send binary dump to browser print $imagedata;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://500146]
Approved by inman
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-18 04:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.