PHP Generated Visual Code Representation
I sometimes get the feeling I should have been a graphics programmer rather than a web programmer. I always seem to get some sort of satisfaction from communicating data visually. Aside from this quickly outdated GoogleGraph class I wrote to wrap the Google Chart API, nothing I’ve created in this field has become useful. But this doesn’t stop me from dreaming up new ways of using the GD libraries.
My first adventure with GD was steganography. I wanted to create a way of hiding data within the color values of randomly generated pixels. I became completely consumed. Over the span of a week I had added more and more methods of encoding until…my brain exploded. SteganoCrypt (I’m a pro at choosing cheesy camel-cased project names) had it all: multiple substitution ciphers, integrity check bits, password protection. All self contained within the pixel colors of a seemingly scrambled image. Maybe one day I will dig it out of my CVS attic and post it here. Until then, have a crack at an encrypted message. If you can manage to decipher it, you’ll win a cookie.
SteganoCrypt Example:

Today I wanted to see what a website would look like after multiple character occurrences are organized and color coded. Each span of color represents the amount of similar characters used throughout the code, placed in order by first occurrence. The more ‘A’s there are, the longer a span of color. As you can see the spans become shorter as the code progresses because less frequently used characters are picked up later in the process. Image width is determined by the amount of total characters in the code. Here is this actual post after processing. I don’t know what character #2F1400 represents, but apparently I use it a lot.
This Blog Post:

While not useful for anything productive, this does give a good perspective of bloat. Though essentially this can be achieved with:
echo strlen(file_get_contents('http://website'));
Google vs. Yahoo vs. MSN respectively:

The three white pixels at the end of each span were added last second to help differentiate colors. Rather than an attached file, I present to you the thrown together code.
$file = file_get_contents('http://ryonsherman.wordpress.com');
$width = strlen($file)/100;
$height = strlen($file)/$width;
$seed = ord($file[strlen($file)/2]);
$img = @imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img, 255, 255, 255);
$chars = array();
for($i = 0; $i < strlen($file); $i++)
(!in_array(ord($file[$i]), array_keys($chars))) ? $chars[ord($file[$i])] = 0 : $chars[ord($file[$i])] += 1;
$x = -1;
$y = 0;
foreach($chars as $index => $count) {
$r = $g = $b = $index;
$g *= $seed * 1.5;
$b *= $seed * 2;
$color = imagecolorallocate($img, $r, $g, $b);
for($i = 0; $i < $count; $i++) {
($x > $width) ? eval('$x = 0; $y++;') : $x++;
imagesetpixel($img, $x, $y, (in_array($i, array($count-1, $count-2, $count-3))) ? $white : $color);
}
}
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);

nice man, nice!