#!/usr/bin/perl #this is a mix of code from various other things and is largely created #by stripping out the bulk of ThousandWords #use strict;#this doesn't like the open/close stuff... whatever use warnings; use GD; #instead of opening an existing image on disk, we are going to create one in memory #then we will pass in randomly generated text for it to write in #then we will feed that image into the rest of this #but instead of printing out to a file, we just print write out to STDOUT my $alphaNummyNumNums = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; #this doesn't account for user error in that some people mistake ones and "I"s #or "G"s nines (lowercase g), "B"s and eights, etc my $randText = ''; my $numChars = 8;#this is how many chars you want to display for(my $bobo = 0; $bobo < 8; $bobo++){ $randText .= substr($alphaNummyNumNums,int(rand(length($alphaNummyNumNums)-1)),1); } #now use GD to create an image that has the same dimensions but takes in the randText my $image = new GD::Image(54,18); my $bgColor = $image->colorAllocate(204,204,153); my $black = $image->colorAllocate(0,0,0); $image->fill(54,18,$bgColor); $image->string(gdSmallFont,3,2,$randText,$black); my ($x, $y) = $image->getBounds(); my $cssStr = 'div{position:absolute;margin:0;width:1px;padding:0;font-size:1px;line-height:1px} body>div{height:1px;}/* hide height from IE6 */'; my $htmlStr = ''; my $xx = 0; my $yy = 0; my $index0; my $index1; my $index2; my ($r0,$g0,$b0); my ($r1,$g1,$b1); my ($r2,$g2,$b2); my $color0 = ''; my $color1 = ''; my $color2 = ''; my $colorTest = ''; my %colorToClass = (); my %classToColor = (); my $pixelCount = $x * $y; my $classCount = 0; my $divCount = 0; for ($yy = 0; $yy < $y; $yy++) { #we are going to jump by 3 and then look at 3 at a time for ($xx = 0; $xx < $x; $xx += 3) { $index0 = $image->getPixel($xx, $yy); $index1 = $image->getPixel(($xx + 1), $yy); $index2 = $image->getPixel(($xx + 2), $yy); ($r0,$g0,$b0) = $image->rgb($index0); ($r1,$g1,$b1) = $image->rgb($index1); ($r2,$g2,$b2) = $image->rgb($index2); $color0 = '#' . sprintf("%.2X%.2X%.2X", $r0, $g0, $b0); $color1 = '#' . sprintf("%.2X%.2X%.2X", $r1, $g1, $b1); $color2 = '#' . sprintf("%.2X%.2X%.2X", $r2, $g2, $b2); $colorTest = $color0 . $color1 . $color2; #now see if this color exists, if it does not, then create one if($colorToClass{$colorTest}){ #this color already exists, so we do not need to create it $htmlStr .= '
 
'; } else{ #this color does not exist yet, so we will create a class for it $classToColor{$classCount} = $colorTest; $colorToClass{$colorTest} = $classCount; $cssStr .= ' .c' . $classCount . '{border-left:1px solid ' . $color0 . ';background:' . $color1 . ';border-right:1px solid ' . $color2 . ';}'; #now add the html to it all $htmlStr .= '
 
'; #bump this up for the next time we use it (when there is a new one needed) ++$classCount; } ++$divCount; } } print "Content-type: text/html\n\n"; print ''; print ''; print $htmlStr; print ''; #print "PixelCount: $pixelCount DIVCount: $divCount ClassCount: $classCount\n";