Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

using GD inside CGI...

by UrbanHick (Sexton)
on Dec 19, 2006 at 17:36 UTC ( #590715=perlquestion: print w/ replies, xml ) Need Help??
UrbanHick has asked for the wisdom of the Perl Monks concerning the following question:

Howdy monks,

This question stems from me trying to figure out how to properly use Perl’s CGI and GD modules.

Basically I am trying to write a cgi that will us GD to draw a black frame around a given image. I want to image to load from a url. So far from the Perldoc I have constructed this:

#!/opt/bin/perl –w use GD; use Image::Size qw(imgsize); use CGI; use LWP::Simple qw(get); my $myImage = get(‘’); my ($x, $y) = imgsize(\$myImage); my $image = GD::Image->newFromJpegData(" +jpg", 1); my $black = $image->colorResolve(0,0,0); $image->rectangle(0,0,$x,$y,$black);

I am now a confused as to how I could print out the $image inside of a CGI rendered webpage like this:

my $cgi = new CGI; print $cgi->header, $cgi->start_html('test page'), $cgi->h1('it’s a framed picture!'), $cgi->end_html;

Many thanks in advanced for all, advice, observations and clarifications.


Comment on using GD inside CGI...
Select or Download Code
Re: using GD inside CGI...
by themage (Friar) on Dec 19, 2006 at 17:45 UTC
    Hi UrbanHick,

    You can create two diferent cgi (or the same cgi called with diferent parameters), one to return the HTML, and the other to return the image.

    Or you can have the cgi that return the html process and save the image to disk, and then in the HTML put the URL to that image. This is what I would do, because this way I already know that the image was correctly processed when I send the HTML to the client.

    The last alternative (that I never tried) is to put the image in base64 (I think) in the HTML. If you prefer this on (I hope not), you can read about it in

    Talking Web
Re: using GD inside CGI...
by shotgunefx (Parson) on Dec 19, 2006 at 18:09 UTC
    You could generate the image and then print a redirect header to the url of it.

    But I must ask, if it's just a black border, wouldn't HTML work better?

    "To be civilized is to deny one's nature."
Re: using GD inside CGI...
by Joost (Canon) on Dec 19, 2006 at 20:27 UTC
    Unless there's a good reason to generate the HTML and the image at the same time, I would use a seperate CGI to just generate the image data:
    # in the HTML generating code: # set up headers and other html... print "<img src='/cgi-bin/my-image.cgi?some=params'>";
    # in the /cgi-bin/my-image.cgi script: # set up the GD::Image in $image print $cgi->header("image/png"); # set the correct content-type binmode STDOUT; print $image->png; # end

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-09-22 02:44 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (177 votes), past polls