in reply to Perl "image" question...

Use Super Search. a quick search finds How can I use a CGI script to return an image?

-Waswas

Replies are listed 'Best First'.
Re: Re: Perl "image" question...
by powerhouse (Friar) on Feb 01, 2003 at 23:01 UTC
    Awesome, the FIRST response on that Thread is what I am trying to do.

    Here is his comment, that I'm referring to:

    ###### Start his Comment....
    I assume you mean "how do I have a CGI program called from an image and have the CGI program pipe an image back to the IMG tag for display"?

    First off is the image tag. Nothing special here, it should looks just like a normal image tag except it calls your CGI and passes the apropriate parameters to it:

    <img src="img_gen.cgi?size=100">

    The interesting part is in your CGI. You need to return apropriate HTTP headers for the image (Content-Type: image/gif or whatever is apropriate). and then just pipe the image contents back. Here's a small CGI program that returns an image, its size based on the passed parameter
    #!/usr/bin/perl -w use strict; use CGI; use GD; my $cgi=new CGI; my $cgi_size=$cgi->param('size') || '50'; print "Content-type: image/gif\n\n"; my $gd=new GD::Image($cgi_size,$cgi_size); my $blue=$gd->colorAllocate(0,0,255); $gd->fill(0,0,$blue); binmode STDOUT; #just in case we're on NT print $gd->gif;

    ####### End his comment


    Ok, that is KIND OF What I'm wanting to do.
    However, I want to use an Image I've already created.
    How do I do that?

    Here is what I have done so far, to no avail:

    This one is just testing, I'll add it to my script when it works
    #!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard :cgi-lib); ReadParse(\%in); if ((defined($in{img}) && $in{img} != 1) || !defined($in{img})) { $image_file = qq~/home/user/home_dir/graphics/banners/FRHWeb_366_x +_60.gif~; open(IMG,"$image_file") or die "could not read image: $!"; binmode IMG; $image_file_code = <IMG>; close(IMG); print "Content-type:image/gif\n\n"; print $image_file_code; exit; } else { $image_file = qq~/home/user/home_dir/graphics/banners/FRHWeb_212_x +_60.gif~; open(IMG,"$image_file") or die "could not read image: $!"; binmode IMG; $image_file_code = <IMG>; close(IMG); print "Content-type:image/gif\n\n"; print $image_file_code; exit; }


    That does not work.
    I would really appreciate it if someone could help me.

    Thanks,
    Richard
      Use the second example (untested)
      open (IMG, $image_file) or die "could not read image: $!";; my ($image, $buff); while (read IMG, $buff, 1024){ $image .= $buff; } close IMG; # assume it's a gif print "Content-type: image/gif\n\n"; print $image;
      poj
        AWESOME poj!
        it works!!

        Thank You!

        thx,
        Richard