<?php 
ob_start
("ob_gzhandler");
function 
make($image){
    if (
function_exists('imagecreatefromgif')) {
        
$size getimagesize($image);
        if( !
$size ) { die ("Error, file is not an image.") ; }
        
$fsizefilesize($image);
        
$type   $size[2];
        
$width  $size[0];
        
$height $size[1];
        if(
$fsize>51200) {die("Image is too big. Max filesize is set to 50 Kbytes.");}
        if(
$width>500) {die("Image is too big. Max width  is set to 500 pixels.");}
        if(
$height>500){die("Image is too big. Max height is set to 500 pixels.");}

        
// find imagetype and such
        
switch ($type)
        {
            case 
1
                @
$im imagecreatefromgif($image);
                break;
            case 
2
                
$im imagecreatefromjpeg($image);
                break;
            case 
3
                
$im imagecreatefrompng($image);
                break;
            default:
                
$im null;
                
$extension="";
        }

        if(@
$im){
            
$imagearray = array();
            for(
$i=0$i<$height$i++){
                for(
$j=0$j<$width$j++){
                    
                    
$rgb ImageColorAt($im$j$i);
                    
$colorrgb imagecolorsforindex($im,$rgb);
                    
$imagearray[($i*$width)+$j] = sprintf("%02X%02X%02X",$colorrgb['red'],$colorrgb['green'],$colorrgb['blue']);
                }
            }

            echo
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" ><head><title>Asciilized Image -- www.mrtwig.net</title><style type=\"text/css\">DIV { line-height:5px;letter-spacing:0px;}</style></head>";
            echo
"<body bgcolor=\"#000000\"><basefont size=\"1\" /><div><pre><font>\n";

            
$k=0;

            for(
$i=0$i<$height$i++){
                for(
$j=0$j<$width$j++){
                    
                    
$ch=rand(97,126);
                    
$ch=chr($ch);
                    if(
$k>&& $imagearray[$k] == $imagearray[($k-1)]){
                        echo 
$ch;
                    }
                    else{
                        echo 
"</font><font color=#" $imagearray[$k] . ">" $ch "";
                    }
                    
$k++;    
                }
                echo
"\n";
            }
            echo
"</font></pre></div></body></html>";
        }
        else{
            die (
"Error, unsupported image type. Only gif, jpg and png are supported.");
        }
    }
    else{
        echo 
"<pre>There was a problem with the GD library installed on the server\n\n";
        echo 
"var_dump(gd_info()); says: \"";
        
var_dump(gd_info());
        echo 
" \"</pre>";
    }
}

if ( 
$_GET['example']=="tux") {  // User clicked the example link. Asciilize your example image.
    
make("images/tux.jpg"); // this is my example image. Exchange for your own, or add more if you wish. Do NOT let the user choose a file from your drive. You don't want him asciilizing your password file :)
}

else if( 
is_uploaded_file($_FILES['ascilize_image']['tmp_name']) ){ // A file has been uploaded, so make the ascii-code    
    
make($_FILES['ascilize_image']['tmp_name']);
}

else{
// A file has NOT been uploaded, so write upload-form html code
    
    
$title"Asciilizer";

    
?>
<table border="0" width="599" cellpadding="3" cellspacing="0">
    <tr>
        <td class="head">
            <h1>Asciilizer.</h1>
        </td>
    </tr>
    <tr>
        <td style="background-color:#b9cbdb">
            <br />Upload the image you want to asciilize. <br />Supported image formats are: gif, jpeg or png. Max filesize is set to 50 Kb and allowed measurements are H&lt;500 px &amp; W&lt;500 px.
            <br />Please, there is no point in trying 2mb screengrabs and such, as it will just fail anyway.
            <br />Keep in mind that the ascii version will be 6 times wider and taller = 36 times the area.
            <br />As such, large images might stall or even crash your browser.
            <br />The optimal image to asciilize, is one with few colors, sharp transitions, around 200x200 pixels, and large areas of the same color, <a href="<?php  echo $_SERVER['PHP_SELF']; ?>?example=tux">like this example</a>.
            <br />
            <br />If you want the sourcecode, <a href="scripts_asciilizer.phps">here it is</a>. Upload it to any php-enabled webserver if you want to play with it. PS: If you wanted to enable larger images, you might need to re-configure your webserver with a larger POST-max-filesize, longer execution-timeout-limit.<br /><br />


         <form enctype="multipart/form-data" action="<?php  echo $_SERVER['PHP_SELF'] ; ?>" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="51200" />
            <input type="file" name="ascilize_image"    size="40" />
            <input type="submit" value="Asciilize it!" />
        </form>
        </td>
    </tr>
</table>
    <?php 

}
?>