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

Hi,

I'm trying to run my first cgi script using gd graphics. Text in the cgi script appears on the web page when the page called via the browser , but no graphics are present on the page. I'm using gd, but the error log suggests I have forgotten to load it. I thought I had installed/loaded it, but given my track record so far, I probably havn't. I hope the following info gives some clue as to what I've done, (I apologise for the format but it seems very difficult to have to put all the br breaks in)

I'm using perl 5.8.1
Summary of my perl5 (revision 5.0 version 8 subversion 1) configuration:

Platform: osname=linux, osvers=2.4.7-10, archname=i686-linux uname='linux localhost.localdomain 2.4.7-10 #1 thu sep 6 17:27:27 +edt 2001 i686 unknown ' config_args='-de' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultipl +icity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LA +RGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', optimize='-O2', cppflags='-fno-strict-aliasing -I/usr/local/include -I/usr/include +/gdbm' ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.1 2.96-98 +)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.2.4.so, so=so, useshrplib=false, libperl=libperl. +a gnulibc_version='2.2.4' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynami +c' cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Characteristics of this binary (from libperl):

Compile-time options: USE_LARGE_FILES Built under linux Compiled at Oct 4 2003 12:00:41 @INC: /usr/local/lib/perl5/5.8.1/i686-linux /usr/local/lib/perl5/5.8.1 /usr/local/lib/perl5/site_perl/5.8.1/i686-linux /usr/local/lib/perl5/site_perl/5.8.1 /usr/local/lib/perl5/site_perl .

The modules that are loaded....

Compress::Zlib -- 1.22 GD -- 2.07 Perl -- 5.8.1 Text::Format -- 0.52

The lib directories....

/usr/local/lib total 6020 drwxr-xr-x 4 root root 4096 Oct 10 05:54 . drwxr-xr-x 14 root root 4096 Oct 7 20:14 .. -rw-r--r-- 1 root root 2774930 Oct 10 05:04 libfreetype.a -rwxr-xr-x 1 root root 738 Oct 10 05:04 libfreetype.la lrwxrwxrwx 1 root root 20 Oct 10 05:04 libfreetype.so + -> libfreetype.so.6.3.4 lrwxrwxrwx 1 root root 20 Oct 10 05:04 libfreetype.so +.6 -> libfreetype.so.6.3.4 -rwxr-xr-x 1 root root 1307842 Oct 10 05:04 libfreetype.so +.6.3.4 -rw-r--r-- 1 root root 821148 Oct 10 05:54 libgd.a -rwxr-xr-x 1 root root 802 Oct 10 05:54 libgd.la lrwxrwxrwx 1 root root 14 Oct 10 05:54 libgd.so -> li +bgd.so.2.0.0 lrwxrwxrwx 1 root root 14 Oct 10 05:54 libgd.so.2 -> +libgd.so.2.0.0 -rwxr-xr-x 1 root root 507927 Oct 10 05:54 libgd.so.2.0.0 -rw-r--r-- 1 root root 219834 Oct 10 05:26 libpng12.a lrwxrwxrwx 1 root root 13 Oct 10 05:26 libpng12.so -> + libpng12.so.0 lrwxrwxrwx 1 root root 19 Oct 10 05:26 libpng12.so.0 +-> libpng12.so.0.1.2.5 -rwxr-xr-x 1 root root 196358 Oct 10 05:26 libpng12.so.0. +1.2.5 lrwxrwxrwx 1 root root 10 Oct 10 05:26 libpng.a -> li +bpng12.a lrwxrwxrwx 1 root root 11 Oct 10 05:26 libpng.so -> l +ibpng.so.3 lrwxrwxrwx 1 root root 17 Oct 10 05:26 libpng.so.3 -> + libpng.so.3.1.2.5 -rwxr-xr-x 1 root root 196358 Oct 10 05:26 libpng.so.3.1. +2.5 -rwxr-xr-x 1 root root 65880 Oct 10 04:55 libz.a drwxr-xr-x 4 root root 4096 Oct 10 15:56 perl5 drwxr-xr-x 2 root root 4096 Oct 10 05:26 pkgconfig

The cgi script...

#!/usr/local/bin/perl use GD; print "Content-type: text/plain","\n\n"; print "Welcome to my Web Server!", "\n"; $remote_host=$ENV{'REMOTE_HOST'}; print "You are visiting from", $remote_host, "."; my $gd = GD::Image->new(400,300); my$white = $gd->colorAllocate(255, 255, 255); my$black = $gd->colorAllocate(0, 0, 0); my$red = $gd->colorAllocate(255, 0, 0); my$green = $gd->colorAllocate(0, 255, 0); my$blue = $gd->colorAllocate(0, 0, 255); my$yellow = $gd->colorAllocate(255, 255, 0); gd->filledRectangle(0, 129, 199, 169, $blue); my $poly = GD::Polygon-new(); $poly->addPt(199, 149); $poly->addPt(399, 74); $poly->addPt(324, 149); $poly->addPt(399, 244); gd->filledPolygon($poly, $yellow); gd->polygon ($poly, $black); gd->arc(199, 149, 250, 250, 0, 360, $red); gd->arc(199, 100, 200, 250, 0, 360, $red); gd->fillToBorder(99, 149, $red, $green); gd->rectangle(0, 0, 399, 299, $red); gd->line(199, 0, 199, 299, $red); gd->line(0, 149, 399, 149, $red); exit(0);

The error...

Can't locate object method "filledRectangle" via package "gd" (perhaps + you forgot to load "gd"?) at /var/www/cgi-bin/GDtest.cgi line 22.

Am I barking up the wrong tree, or just plain barking mad?

janitored yb ybiC: Formatting clean-up using balanced <code> and <p> tags

Replies are listed 'Best First'.
Re: httpd error_log (perhaps you forgot to load "gd"?)
by MidLifeXis (Monsignor) on Oct 10, 2003 at 17:45 UTC

    You are missing the '$' on quite a few of your uses of $gd. Go to line 22 (as stated by the error) and check it out. That is not the only one.

      MidLifeXis,

      Thank you, the code now runs without error.

      Clearly, I need glasses.
Re: httpd error_log (perhaps you forgot to load "gd"?)
by jasonk (Parson) on Oct 10, 2003 at 19:37 UTC

    Besides the missing $'s, you also aren't doing anything with the $gd object you created, you can't simply start drawing and expect that GD will know how to get that image to the web browser. What you will have to do is either have GD save the image you are creating to a file, or have a separate script that draws and outputs the image, but either way you will need to include an <img> tag in the html that you are generating to indicate to the web browser where to get the image from and where to display it in the html.


    We're not surrounded, we're in a target-rich environment!
      jasonk,

      You are absolutely right, the code now runs without error but no graphics appear on the web page. Thank you for the education.

      Don't suppose you've seen any sample scripts that do what you describe?

      Thanks again.

        Your code has almost everything you need to accomplish it, you just need to save the image somewhere and include it in the page. You will need to setup a directory your script can write files to, which is also accessible from the web, but how to do that is dependent on your web server...

        my $img = join('.',time(),$$,'png'); open(OUT,">tmpimages/$img"); print OUT $gd->png; close(OUT); print "<IMG SRC=\"$img\">\n";

        Of course you will also have to change your script to emit text/html rather than text/plain for a content-type.


        We're not surrounded, we're in a target-rich environment!