Get Thunderbird! Firefox 2

Misc. Stats For This Page

Operating Systems
Linux
Windows
Mac
Other

Browsers
Firefox
MSIE
Netscape
AOL
Safari
Opera
MSN
Search Bots
Other

Google
Web www.ckorp.net
Page loads - 9662.
This page last modified on 01 Nov 2007
Foxkeh banners for Firefox 2
.c.o.n.v.e.r.t.  .a.n.  .i.m.a.g.e.  .t.o.  .a.s.c.i.i.  .a.r.t.
Have you ever seen ASCII art? In case you haven't it basically is the creation of art using only ASCII characters. Do a quick Google for "ascii art" and you will see what I am referring to. I have often admired these creations and adaptations and wondered if I could write a php script to create these from a normal image.

This script was done more as a "proof-of-concept" rather than a functional script so please accept it as that. I am certain that it can be modified to provide a much more refined output.

I need to reiterate here that I do all of my development in Firefox the output may look completely different in other browsers

Below you should see the input image, directly below that the newly created ASCII image, and the directly the ASCII image the code used to create it.

%%%%%%%%%%%%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%#%%%%%%%%%########%%#%%%%%%%%%#######%%%%%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%############%%%%############%%#%%##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%#############%%############%%%#%#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%####%%############################%%%#######%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%###################################%%%%#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%###################################%%%%%#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%############%#####################%%%%%%%%##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%#############%%#################%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%################%#####%%%%%#%%%%%#%##%%++**++%%%%%%%%%%%%%%%%%%%%%%%%%%%%##%###
###########%#######%##%%%%%%####%%#%%%**;;;;;**++++%%%%%%%%%%%%%%%%%%%%%%#######
#######%%%%%%######%%%%+%++%%%%%%%%++*;;***+++****++*++%%%%%%%%%%%%%%%%%########
######%%%%%%%%%%##%%%%+%%%%%#%%#%%++*:;*++++*******+****++%%%%%%%%%%%%##########
######%%%%%%%%%%%%%%%%%%%%%%%%%%%%++;:;++**;*****;**++*;++%%%%%%%%%%%###########
########%%%%%%%%%%%%%+%%%%%%%%%%%%+*::***;;;**********+++++%%%%%%%%%%###########
########%##%%%%%%%%%%+%%%%%%%%%%+++*:;+*;:;*****+**++**+++++%%%%%%%%%###########
##########%%%%%%%%%%%%%%%%%%++%+++*;:;;;:;;;*;**:,,::;**+++++%%%%%%#############
#######%##%%%%%%%%%%%+++++++++++***:;;::;;;;::,,.,,::::;*+++++%%%%##############
#######%%%%%%%%%%%%%%+++++++******;::;::;;;:,,,,:::::;;;;;*+++%%%%##############
######%%%%%%%%%%%%+%%+++++++****;;;::;:;;;:,,:::,,,::;;;;;;*+++%%###############
#####%%%%%%%%%%%%%+++++++***;;;;;::::;;;;::::;;:,,,::;;;;;;;*++%%%##############
####%%%%%%%%%%%%%%+++++++*;::,:;;:::::::::::;;;::,,::::::;;;;+++%%##############
###%%%%%%%%%%%%%%%+%%++++*;:,,::;:::,:::,,,::;;;;::,,,,,::;*;*++%%##############
###%%%%%%%%%%%%%%%%%++++*;:,,,:::::,,,,,,,,,,:::;;;::,,,,,:;;;;++%%#############
#####%%%%%%%%%%%%%++++++**:::::,,,,,,,,.,,,,,,,:::;;;;::,::::::;+%%#############
###%%%%%%%%%%%%%%%++++++**;:::,,,,,,,........,,:,:::;;;;;:,:;;::*%%#############
%%%%%%%%%%%%%%%%%%%++++**;:::,,,,,.............,,,:::;::::,,:;*:;+%#############
%%#%%%%%%%%%%%%%%+++%+**;;::,,,,,,,.............,,,,:::::,,,,:*;:*%#############
###%%%%%%%%%%%%%%**+++*;::,,,...........``........,,,,::::,,,.:*;;%#############
###%%%%%%%%%#%%+*;*+*;;:,,,,........``````````.....,,,,,,:,,,.,:*;+%############
###%%%%%%%%%%%+*;*++;::,,,,,.........```````````....,,,,,,,,,...;;;+############
##%%%%%%%%%%%%+;*++;;;:,,,,.....`...````````........,,,,,,,,,,.,:;:+############
##%%%%%%%%%%++**+++*;:,,,......```````````````........,,,,:,,...,;;*%###########
%##%%%%%%%%%++++%+**;:,,.......````.``````.``..`.......,,,,,,...,:;*+%##########
####%%%%%%%++++%++**;:,,.......``````````````...........,,,,,....,;*%%%%########
#####%%%+++++%%%++*;::,,........``````````````..........,,,,,.....:;%%%%########
###%%%%++++++%%%+*;;::,,........```````````.````.........,,,......,:+###%#######
####%++*+%%%++%%+*;;::,.........``..````...`..............,,..`...,;*%##%%######
####%+*++%%%%+%%+*;;;;:,,.......``.....,..``..............,,..`..,,;*+##########
###%++++%%+%+++%+*++++*;,,......``..,:;;*:,::,............,,.```.,,:*+%###%#####
###%+%%++++%+*+%*+%#%+**:,......`...,,:;*****;,.....,....,,..```.,::;+++#%%%####
###%+%%%+++%+*+%++%++*;:,.......`......,:;:::,............,..```.,::;*+%%%%%####
##%%+%%++++%**+%++*;;;:,,,..........```..........,,..........```.,,:;*+%%%######
##%%+%++++++*;+%*;:,::::,,....`..``.`...``.....`.,,..........```.,:;;;*+%%######
##%%%%++++++*;+%*:,,:,,,:,,...````...............,,,.........```.,::;**++%######
###%%%+*++%%**++;,,,;;;::::,..````.....,,,,,.....,,,.........`````.,;****+%#####
###%%%++++%+**%+:,,:***;:;;;..`.......,:;;;;:,.`.,:,.........`````..:;;*;*+%####
###%%%+*++%+*+%+:,:****;::;*:.`.....,,,:;;:;;;,..,::,.......`````.`.,:;;;;*+####
###%%%++++++*+%*::;++***;::*;..`...,,,:;*;;:;::,.,::,...,...`````...,::;;;**+###
####%%++%+%+*+%*;;+*,:**;;:**,....,::;;:,;++*;:,,,,,,,,,,...`````..,.,;;;;*+++%#
#####%++%+++;+%*;**:,+%%;;:**,...,,,::,.;+%+;*:,,,,,,,,,,,..`````.,,:::;:;;++%%%
####%%++++++*++*;;*;:%%+;;;*;,...,,.,,,.;%%+:;::,,,,,..,,,...``...,,:::::;;*+%%%
####%%++%%%+*++*;;**;*+;;:;*:,,,,,,.,:::;++*:::,,,,,,..,,,.....,,,,,,:;::;***%%%
####%%++%%%++%+*;*+++*;;::;+:,,,,,,,::::;;:;;;:,:,..,..,,,.....,,,,::::;::*+*+%%
####%%++%%%%+%+++*;***;::*++:,,,,,,,,::::::;:::::,..,,.,,,,...,,,:,,,,::;:;+*+%%
#####%++%%%%%%+*+*;;:::::*%*:,,,,,,,.,,:,,,,,:::,,..,,,,,,,.,,,,,:;;::::;;:*++%#
####%%%%%#%#%%+**;;:::;::*%;,,,:,,,,,.,,:,,,,:::,,..,,,,,:,,,:::,,**;;:::::;*++%
####%%%%%%###%+;;::,,,:::++;:,::::,,,..,,,,,,,:,,,,,::,,:::::,:;,.:;**;:::::**+%
####%%%%#%###%+;:::,:,,,;+*::,::;::,,...,..,,,,,,,,,:::::::::,:;,.::;+*;:,,,;**+
####%####%%##%*::,,,:,,:*+;::,::;::,,,,..........,,,::::::;*;:;:..::;**;:,,,;;;+
#########%%##%*;,,::,,,;+*:,,,::;;:,,,,...........,,:::::;**;::,`.,:*;;::.,.:;:;
##########%##%*::,:::::;+:,,,,,:;;;:,..,...........,:;;;:;**::,,...,;,:;:...:;::
#############%*::,:::::*+:,,.,,,:;*;:.............,::;;;;;*+:,......,.:*:.`.:;:,
##############+::::::,,+*:,...,,:::;:,...........,,::;;;;;**::.........*:.``,;:,
##############+;::;:,,;++;,,..,,:,,:;:,..........,,,:;;;;***;,....:,`..;:```,;:,
##############+;;::,,,*%%*:,,,::::,,;:,,.........,,,:;*;;***;,,,.,:,...;:``.,;;:
##############+**;:,.,;+%%*:::;;;;;;;,,,,......,,,,,:;*;;***:..,,:,,..,:,``.,**:
##############+**;:,.,:;+%+*;;::,,:::,,,,,...,,,,,,,:**;;***,..,::,,,,,;.``,:**:
##############+*+;,,,:::;;;::::,:,:,,,,,,,,..,,,,.,,:**;;;**,..:;;:,.,:;...,:+*;
##############+*+;:,:::,,,,,,::,,,,,,,,,,:,,.,,,,..,:*;;;;*;,.,;*;*;,:::...:;+**
##############+*+;:::::,,,,,,,,..,,,,,,,::,,..,,,.,,:*;:;;*;,,,;****;;;::,,;;+*+
##############+*+*;::,,,,,,.,,,.,.,,.,,,,,,,,.,,,.,,:*;;:;*:.,,;*+***;::;;,;;++%
##############%*+**;::,,,,,,,,,.,,,,,,,.,,,,,,,,...,:*;::;*:.,:*+%+*;;;*++;*%###
##############%*+***;::,,,,..,,,,,,,:,,,,,,,.......,:;:,::**::*%##%+***+%%++####
##############%+*****;;::::::::,,::::,,,,,,........,:;:,,:*%++%#####%%%###%#####
###############+*;:*+++****;;*;:;;;:;:::,,,.,......,:;:,,;*%####################
###############++*::*+%%%+++++++*;;;+***;:,,,.......:;,.:;+%####################
###############%+*:,:*+%%+**;***;;:;*+%+*:,,,....`..:;..:*%#####################
###############%*+;,,:*%+**;;;;;::::;%%+;:,,,....`..::.,*+######################
################++*:,,;%+;;;:;::::,:*%%*:,,:,...`..,:,.:+#######################
################%;*:,,:%+;;:;::::::;++;:,,,:,,,..`..:.,*########################
################%**;,.:%*:::::::;::***;:..,,.,,..`.:,,;%########################
#################%**:.:%*::::::;;,:**;;,...,.,,.`..:.;%#########################
#################%**:.,%+:::::;;;,;+*;;,...,.,,.`.,,;%#########################%
##################+*;,,+%;:::;;;;:*+*;:,,....,,...:;+#########################%%
##################%**:,*%;:::;;;;:**;:,,,....,.`.:*%########################%%++
###################+*;,*#*::;;;;::*;:,,,.....,..,*%########################%%+++
###################%**:;#*;:;;;;:;*:,,,......,..;%@########################%%+++
####################++;;#%;:;;;;:*;,,,......,..:##########################%%++++
####################%%*;+#*:::;:;;:,,...`..,,,:%##########################%+++++
######################%**#%;;;:;*:.........,,;%#########################%%++++++
#######################+;+#*;;:*;,........,:;%##########################%+++++**
########################+*%%****,....,....:*###########################%%+++++**
########################%*+#%++:,.......,:+###########################%%++++++**
%%#######################%++%*;,,......,;%###########################%%+++++++++
%%%###############@#####@%%*;:,,,,,..,,*%##@#########################%+%++++**++
%%#########################+*;:::::,,:+############################%%+++++++++++
%###########################%+*;:;::*%#############################%%+++++++*++%
#############################%%++++%%#############################%++++++++**++%
#########################%%###%+++###############################%%+++++++++++%%
########################%;:*%#%++%##############################%+++++++++++++%%
########################+::;+%%%################################++++++++%++++%%%
#######################%;*%##%+%#######@#######################%++++*+++%%++%%%%
######################%++%#%##################################%+++++*++++++%%%%%
##################@###%*######################################%+*++++++++++%%%%%
######################%%####################################%%+++++*+****+++%%%%
####################%##%###################################%+***;;*;*;*;****++%%
###########################################################%+*************+++%%%
###################%######################################%%+************+++++%+
#################%%%#####################################%%%++**++***+++++%%%%%%
################%%%#####################################%%%%++++++++++++%%%%%%%%
###############%%#######################################%%%++++++***++++%%%%%%%%
###############%%######################################%%%%+++*++**++++%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%##############################%%%%++++++++++++%%%%%%%%%

<?php
  $ascii_chars 
= array("@""#""%""+""*"";"":"","".""`"" ");
  
$image imagecreatefromjpeg("einstein_tongue.jpg");
  
$width imagesx($image);
  
$height imagesy($image);
  for(
$y=0$y<$height; ++$y){
    echo 
"<div style=\"position:relative;top:-".($y*7)."px;height:5px;line-height:5px;\">";
    echo 
"<pre style=\"font-size:5px;line-height:5px;letter-spacing:0px;\">";
    for(
$x=0$x<$width; ++$x){
      
$this_color imagecolorat($image$x$y);
      
$rgb imagecolorsforindex($image$this_color);
      
$brightness $rgb["red"] + $rgb["green"] + $rgb["blue"];
      
$brightness round($brightness 85);
      
$char $ascii_chars[$brightness];
      echo 
"<font color=\"#".dechex($rgb["red"]).dechex($rgb["green"]).dechex($rgb["blue"])."\">".$char."</font>";
    }
    echo 
"</pre>";
    echo 
"</div>\n";
  }
?>

The code is pretty self explanatory so I won't go into to much detail here. In short, the script scans the image pixel by pixel. It then combines the total of the red, green, and blue (RGB) colors, divides them by a preset number, and rounds to the nearest integer. The replacement character is chosen by using the result as the key for the $ascii_chars array. A few points to keep in mind

  • The ASCII image will be considerably larger (height and width) than the original. In my script, I used a relatively positioned DIV to further condense the output by reducing the space between the lines. This is not a necessity, I just did it because I thought it was more visually appealing.
  • The characters in the $ascii_chars array can be changed to suit your needs. They are arranged by "weight" from heaviest to lightest. By "weight", I mean characters that represent darker pixels are first (the more space the character takes up the darker it appears in the ASCII image) in the array.
  • This script does "attempt" to re-create the colors used in the image. On most gray scale or black and white images it seems to work fine but in true color images everything is a bit off.
  • Since it reads pixel by pixel and recreates it with a much larger character, any imperfections or color oddities are magnified.