ascii art with PHP GD extensions
- Tim Brockman
- @ 10:07 pm
My personal fondness for ASCII art began on the BSU VAX/VMS terminals back in 1993. I found a sweet Spock portrait to append to my login script. It made me smile with crazy nerdmusment every time I logged into the account.
As any good subgenius, I'll start with the Dobbshead. This is small, only 66x100 because each pixel = one letter (or space). To further simplify things, I posterized this gif so it only has 3 colors; white, gray, and black. I didn't want to write a heavy edge detection algorithm.
Display in (x)HTML
First thing first, if you're going to post ASCII art on the web you should use the <pre> tag (preformat) or the <tt> (type/term text) tags which, should give you a fixed width font. They will make sure all of the letters take up the same space. If you have some other tags nested inside these tags they probably won't work.
GD functions
The GD and Imlib2 extensions allow you to create images dynamically using PHP. Check your phpinfo() for enabled GD support. This script uses 6 functions.
- imagecreatefromgif() creates an image and returns a handle
- imagesx() gives you the x dimensions of the image
- imagesy() gives you the y dimensions of the image
- imagedestroy() destroys the image when you're done
- imagecolorat() gets index of the color at specific x y coordinates
- imagecolorsforindex() converts indexed colors to rgb (and alpha) values 0-255
The program works by creating an image form a source gif file. Once this is done we can loop through the pixels and output an ASCII character for each pixel. We'll decide which characters based on the rgb values 255, 255, 255 is white represented by a space if the background is white, and 0, 0, 0 is black represented by an M or W usually. Anything in between can be other characters like 0 or Z or \ depending on how dark you want it.
The Code
<?
// displays gif as ascii art
$the_image = imagecreatefromgif("images/dobbshead.gif");
$xlimit = imagesx($the_image);
$ylimit = imagesy($the_image);
print("<tt>\n");
for($ycurrent=0; $ycurrent < $ylimit; $ycurrent++){
for($xcurrent=0; $xcurrent < $xlimit; $xcurrent++){
$current_color = imagecolorat($the_image, $xcurrent, $ycurrent);
$rgb = imagecolorsforindex($the_image, $current_color);
$color_score = $rgb['red'] + $rgb['green'] + $rgb['blue'];
if($color_score < 300){
//black color
print("M");
}elseif($color_score > 600){
//white color
print(" ");
}else{
//gray color
print("$");
}
// print($color_score);
}
print("<br />\n");
}
print("</tt>\n");
imagedestroy($the_image);
?>
In Conclusion
The output can be found at ASCII Dobbshead. This is just the beginning of course. You could use some basic edge detection by comparing pixels to their neighbors to create some really intricate ASCII art. Let me know if you do a good Spock.
For more ASCII stuff check out Chris Johnson's ASCII art collection
As expressed in an earlier post, I wasn’t entirely satisfied with this site. As much as I enjoy the simplicity of this design, I felt the need to add an element of random. I also wanted a basic idea to discuss without getting to deep. Hence, the randomly generated logo-esque graphic in the upper right …continue reading…
Further development of my real estate content management system took place this week. Built 2 more sites using the system to see what problems I’d encounter. So far just a couple minor things that were fixed. Generally these sites will take a couple hours to deploy. Currently theres only the one skin/template so they look …continue reading…
This week CelebrationHomes.info is going to be launched (in a matter of speaking). What I mean by that is more a matter of beginning the organic process of promotion and field testing. The sitemaps are up and functioning, Google is beginning to index the site. It has social networking bookmarks from addthis. With a little …continue reading…
Here’s a link to the Facebook Sketch Crawl event hope to see you there.

Contact PHÓ