Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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="http://www.mysite.com/cgi.bin/myscript.cgi?display_this_imag +e=myimage.gif">

NOT

<img src="http://www.mysite.com/myimage.gif">

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

Comment on Serving images with perl
Select or Download Code
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 cgi.pl.


    Evan Carroll
    www.EvanCarroll.com
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,
      -Adam

    --
    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 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.
      Thanks.
        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=\"http://www.caralan.com/cgi-bin/customer_image_server.cg +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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-10-25 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls