demo.image

Graphics programming with GD

This demonstrates how you can create a webpage with an inlined GIF image which itself is generated on-the-fly from within this page. In other words: the image is part of the webpage source, too. The trick here is that the complete page is surrounded with an ePerl block and according to the QUERY_STRING the script either produces the page itself (pure HTML) or the image by programming it on-the-fly with the GD module. The link to the image is done via a self-referencing URL.

As an example, we present an on-the-fly generared GIF image showing the Web216 color palette (the ``browser-safe'' colors):

The Image

interlaced('true'); for (my $b = 0; $b <= 255; $b += 51) { for (my $r = 0; $r <= 255; $r += 51) { for (my $g = 0; $g <= 255; $g += 51) { my $x = (($b / 51) % 3)*60 + (60-($r / 51) * 10); my $y = (($b / 51) < 3 ? 0 : 1)*60 + (60-($g / 51) * 10); my $col = $im->colorAllocate($r,$g,$b); $im->rectangle($x, $y, $x+9, $y+9, $col); $im->fill($x+2, $y+2, $col); } } } my $C = $im->gif; print "Content-type: image/gif\n"; printf "Content-length: %d\n", length($C); print "\n"; print $C; } !>