Convert a php image to an ascii output image
Convert a php image to an ascii output image
This PHP function creates a simple JPG image ASCII art rendering. It uses Guangdong for heavy work, and some simple shift distribution colors. The color is then transferred to the hexadecimal value and the generated text is output to the browser. ASCII code from image art to PHP now, it's easy. If you think you have any improvement, please feel free to contact us for your idea PHPRO.
<? 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;
?>