http://www.perlmonks.org?node_id=106424


in reply to Random Pictures Using .pngs ... how?

GD can do this (and more). Here's a quick 'n' dirty example that will create a 100x100 pixel PNG and spew it to STDOUT (so make sure you redirect to a file in your shell).

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(r +and(256))); } for my $x (0..99) { for my $y (0..99) { $im->setPixel($x,$y,$colors[int(rand(255))]); } } binmode STDOUT; print $im->png;
"One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison

Replies are listed 'Best First'.
Re (tilly) 2: Random Pictures Using .pngs ... how?
by tilly (Archbishop) on Aug 21, 2001 at 04:37 UTC
    Before your print add:
    print "Content-Type: image/png\n\n";
    and your script is now a CGI script that can be put behind a webserver. There is now no need for a temporary file!
Re: Re: Random Pictures Using .pngs ... how?
by bladx (Chaplain) on Aug 21, 2001 at 08:48 UTC
    Thanks, myocom for the quick intro to the awesome GD module! It is a very useful module for me, and the documentation is very lengthy, however very useful.

    Thanks tilly for showing me how to make this useful for CGI, since I tend to program in CGI more often than the shell usually.

    I will be continuing to experiment with the GD module, and see if I can't get really good at utilizing this module for some really cool Perl artwork *hopefully*!

    Andy Summers