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

Hi, i'm attempting to use a <img src="example.pl"> to execute my script from a seperate location. I know that this works in executing the script (I wish to collect statistics on who visits my web blog and this is the only way I can see to do this) It does however only display the usual 'no real picture' white box with red x. Can anyone point me in the direction of a module that allows me to display an actual picture? Or is it as simple as just printing jpeg (or similar) encoded information out via the normal method of printing text.

Replies are listed 'Best First'.
Re: Using perl in <img src=> scenarios
by friedo (Prior) on Aug 27, 2006 at 02:18 UTC
    If you don't want to do an internal redirect (or aren't using Apache) then you can just set the MIME type and output a small image file. (A 1x1 transparent GIF is a good idea.)

    #!/usr/bin/perl use strict; use warnings; use CGI; # do statistics stuff... my $q = CGI->new; print $q->header( 'image/gif' ); # slurp file and output local $/; open my $fh, 'tiny_img.gif' or die $!; print <$fh>;
Re: Using perl in <img src=> scenarios
by Joost (Canon) on Aug 27, 2006 at 01:53 UTC
Re: Using perl in <img src=> scenarios
by gellyfish (Monsignor) on Aug 27, 2006 at 09:06 UTC

    Just for fun here is how you might output a 1x1 GIF if you don't want to be bothered with an external file. The big block of gibberish after the __DATA__ is the uuencoded image data so you should be careful not to mangle it, note the single quote on a line by itself at the end particularly:

    #!/usr/bin/perl -w use strict; use CGI qw(:standard); my $image = unpack "u*",do { local $/; <DATA>}; print header(-type => 'image/gif'); print $image; __DATA__ M1TE&.#=A`0`!`.<```````$!`0("`@,#`P0$!`4%!08&!@<'!P@("`D)"0H* M"@L+"PP,#`T-#0X.#@\/#Q`0$!$1$1(2$A,3$Q04%!45%186%A<7%Q@8&!D9 M&1H:&AL;&QP<'!T='1X>'A\?'R`@("$A(2(B(B,C(R0D)"4E)28F)B<G)R@H M*"DI*2HJ*BLK*RPL+"TM+2XN+B\O+S`P,#$Q,3(R,C,S,S0T-#4U-38V-C<W M-S@X.#DY.3HZ.CL[.SP\/#T]/3X^/C\_/T!`0$%!04)"0D-#0T1$1$5%149& M1D='1TA(2$E)24I*2DM+2TQ,3$U-34Y.3D]/3U!04%%145)24E-34U145%55 M55965E=75UA86%E965I:6EM;6UQ<7%U=75Y>7E]?7V!@8&%A86)B8F-C8V1D M9&5E969F9F=G9VAH:&EI:6IJ:FMK:VQL;&UM;6YN;F]O;W!P<'%Q<7)R<G-S M<W1T='5U=79V=G=W=WAX>'EY>7IZ>GM[>WQ\?'U]?7Y^?G]_?X"`@(&!@8*" M@H.#@X2$A(6%A8:&AH>'AXB(B(F)B8J*BHN+BXR,C(V-C8Z.CH^/CY"0D)&1 MD9*2DI.3DY24E)65E9:6EI>7EYB8F)F9F9J:FIN;FYR<G)V=G9Z>GI^?GZ"@ MH*&AH:*BHJ.CHZ2DI*6EI::FIJ>GIZBHJ*FIJ:JJJJNKJZRLK*VMK:ZNKJ^O MK["PL+&QL;*RLK.SL[2TM+6UM;:VMK>WM[BXN+FYN;JZNKN[N[R\O+V]O;Z^ MOK^_O\#`P,'!P<+"PL/#P\3$Q,7%Q<;&QL?'Q\C(R,G)R<K*RLO+R\S,S,W- MS<[.SL_/S]#0T-'1T=+2TM/3T]34U-75U=;6UM?7U]C8V-G9V=K:VMO;V]S< MW-W=W=[>WM_?W^#@X.'AX>+BXN/CX^3DY.7EY>;FYN?GY^CHZ.GIZ>KJZNOK MZ^SL[.WM[>[N[N_O[_#P\/'Q\?+R\O/S\_3T]/7U]?;V]O?W]_CX^/GY^?KZ B^OO[^_S\_/W]_?[^_O___RP``````0`!```(!`#_!00`.P`` `

    /J\

Re: Using perl in <img src=> scenarios
by shotgunefx (Parson) on Aug 27, 2006 at 03:25 UTC
    Just specify a width and height of 1. If you want the GIF part of the script, you can modify the following snippet On-demand single-pixel GIFs

    -Lee
    "To be civilized is to deny one's nature."
Re: Using perl in <img src=> scenarios
by blue_cowdawg (Monsignor) on Aug 27, 2006 at 22:04 UTC
        Can anyone point me in the direction of a module that allows me to display an actual picture?

    Have you looked at GD and friends? It's sometimes a PITA to build but I've used it in the past to create images "on the fly" and display them for various reasons. IIRC you can do some neat stuff with Image::Magick as well although I've no experience myself with that module.

    With GD the trick is to emit a line "Content-type: image/{image type}" and putting stdout into binary mode before emitting the image.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg