<?php

/*
   One day I was playing around browsing through some old files of mine and 
   I found some very, very old ASCII art. Looking at them, it got me thinking 
   about how to create those images and I figured I'd give it a shot -- the 
   result is this script. Given an image of any size in PNG format, this 
   "ASCII-Art" script will create a full-color rendering of that image using 
   only ASCII characters.
*/

    
function image2ascii($image_r$fixed true) {

        
$chars = array("M""@""#""*""+"":"".""," "&nbsp;");
        
$width imagesx($image_r);
        
$height imagesy($image_r);
        
        
$retval "";
        
        for( 
$y 0$y $height$y++) {

            for( 
$x 0$x $width$x++) {
                
                 
$rgb imagecolorsforindex($image_rimagecolorat($image_r$x$y));
                 
$brightness $rgb['red'] + $rgb['green'] + $rgb['blue'];
                 
$char = ($fixed) ? "#" $chars[round($brightness 100)];
                
                 
$hexval sprintf("%02X%02X%02X"$rgb['red'], $rgb['green'], $rgb['blue']);
                 
$retval .= "<font style='font-size: 6pt; font-family: \"Courier New\", Courier, mono;' color=#$hexval>$char</font>";
                
            }
             
$retval .= "<BR/>\n";
        }
        
        return 
$retval;
    }

    if(isset(
$_FILES['image']['tmp_name']) &&
        
is_uploaded_file($_FILES['image']['tmp_name'])) {
            
        
$filename $_FILES['image']['tmp_name'];
    
    } else {
    
        
$filename "./images/php.png";
    
    }

    echo 
image2ascii(imagecreatefrompng($filename), true);
?>