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

Ok, things have been going well with learning about how to use the nice GD module to create random images, etc. However, I have stumbled accross a problem I was having earlier, but didn't say anything much about until now. I have the following 2 files, and here they are in their basic code form:

File: gd1.pl
#!/usr/bin/perl -w use strict; use GD; my $im = new GD::Image(100,100); my @colors; # Preallocate colors, since PNG has a limited number of co +lors for (0..255) { $colors[$_] = $im->colorAllocate(int(rand(256)),int(rand(256)),int(ran +d(256))); } for my $x (0..99) { for my $y (0..99) { $im->setPixel($x,$y,$colors[int(rand(255))]); } } print "Content-Type: image/png\n\n"; print $im->png;


File: gd2.pl
#!/usr/bin/perl -w print "Content-type: text/html\n\n"; print<<HTML; <html><head><title>testing more</title></head> <body bgcolor=#ffffff> <center> HTML print "Content-type: image/png\n\n"; print "<img src='gd1.pl' height=100 width=100>\n"; print<<HTML; </center> </body> </html> HTML


And I was trying to figure out how I can use regular HTML code to run this random png image creator program over the internet, so it would look like it was a regular png already on the server.

My guess is there is something really wrong with the gd2.pl file since I haven't tweaked the original gd1.pl actual program file all that much.

Thanks for any help or input I could get!

Andy Summers

Replies are listed 'Best First'.
Re: Displaying randomly created png's in HTML
by count0 (Friar) on Aug 21, 2001 at 20:54 UTC
    There are three simple methods that come to mind to include the gd1.pl image in your html.

    1) Exactly the same as in your gd2.pl cgi. <IMG src="gd1.pl">
    And if you want it to show up in the html as a .png file:
    2) Write the png to a file every now and then (such as with a crontab periodic run of gd1.pl, or with an SSI call to gd1.pl)
    3) If you run the web server yourself, modify the way .png files are served. Set it up to treat all .png files as perl, or you can even set it up to only do that in a specific directory. See your web server (hopefully Apache ;) docs for details on doing this.
Re: Displaying randomly created png's in HTML
by Jouke (Curate) on Aug 21, 2001 at 21:56 UTC
    OK, a few hints:
    1. You want to set binmode for STDOUT before you write the $im->png().

    2. The html script should look like this:
    #!/usr/bin/perl -w print "Content-type: text/html\n\n"; print<<HTML; <html><head><title>testing more</title></head> <body bgcolor=#ffffff> <center> <img src='gd1.pl' height=100 width=100> </center> </body> </html> HTML
    HTH

    Jouke Visser, Perl 'Adept'
    Using Perl to help the disabled: pVoice and pStory
Re: Displaying randomly created png's in HTML
by Beatnik (Parson) on Aug 21, 2001 at 22:31 UTC
    Explaining how HTTP works might give you better understanding.

    Basically a browser fetches a page (for arguments sake call it page.html) with a GET request (webserver sends as content-type text/html). It then analyzes the file for IMG tags, EMBED tags, background tags in TABLE and BODY, etc and it converts those relative filenames into full URLs (if relative filenames are used). Then it fetches those files one by one and inserts em into the page. Text browsers like Lynx (which is libwww based, like LWP::Simple etc) don't automagically fetch all files . They don't display images, play soundfiles or shockwave animations.

    Bottom line is that your images are treated as images, thru the HTTP header the webserver sends, same header that is generated by your script. If your script doesn't generate a content-type header (or a invalid one for the data), the webserver doesn't know what content-type it is and neither does your browser. Regular files have valid content-type headers since that's directly handled by the webserver.

    There is a request for each file that needs to be downloaded. So you can't insert HTML tags in image/png data, since the HTML page was handled in a previous request (as content-type text/html). You can't mix different content-types in the same script call. Once the HTTP header is set, the remaining data is handled as belonging to that specific content-type data.

    Hope this makes sense... This is only part of the HTTP protocol, things will get clearer once you're used to the basic stuff.

    Note: There are ofcourse POST requests, but that's a whole new chapter.

    If you want to know more about HTTP, you can check RFC 2616, which are the specs for HTTP 1.1 (1.0 is still more commonly used but it'll give ya an idea). Ofcourse you can always just learn while you go, which is more fun.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Displaying randomly created png's in HTML
by bladx (Chaplain) on Aug 22, 2001 at 00:24 UTC
    Ok 2 things:
    Just for your information, and (where the credit is due,), the gd1.pl file came from this node by myocom at gd1.pl ... thanks myocom for getting me started on this path of working with the GD module!

    Also, I apologize for the code which is very poorly spaced for some reason! When I previewed it, it was just fine, but my browser some how joo'ed it up... i'm using Opera 5.0 , but it still messed it up... does anyone know why my code got messed up when it looked fine in the preview page?

    Andy Summers