<? Php
Function image2ascii ($ image)
{
// Return value
$ Ret = '';
// Open the image
$ Img = ImageCreateFromJpeg ($ image );
// Get width and height
$ Width = imagesx ($ img );
$ Height = imagesy ($ img );
// Loop for height
For ($ h = 0; $ h <$ height; $ h ++)
{
// Loop for height
For ($ w = 0; $ w <= $ width; $ w ++)
{
// Add color
$ Rgb = ImageColorAt ($ img, $ w, $ h );
$ R = ($ rgb> 16) & 0xFF;
$ G = ($ rgb> 8) & 0xFF;
$ B = $ rgb & 0xFF;
// Create a hex value from the rgb
$ Hex = '#'. str_pad (dechex ($ r), 2, '0', STR_PAD_LEFT ). str_pad (dechex ($ g), 2, '0', STR_PAD_LEFT ). str_pad (dechex ($ B), 2, '0', STR_PAD_LEFT );
// Now add to the return string and we are done
If ($ w = $ width)
{
$ Ret. = '<br> ';
}
Else
{
$ Ret. = '<span style = "color:'. $ hex. ';" >#</span> ';
}
}
}
Return $ ret;
}
?>
Example Usage
<? Php
// An image to convert
$ Image = 'test.jpg ';
// Do the conversion
$ Ascii = image2ascii ($ image );
// And show the world
Echo $ ascii;
?>