Php image conversion to ascii output Image _ PHP Tutorial

Source: Internet
Author: User
Convert a php image to an ascii output image. Php image to ascii output image this PHP function creates a simple JPG image ASCII art rendering. It uses Guangdong for heavy work and converts php images into ascii output images.

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.


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. ='
';
}
Else
{
$ Ret. = '#';
}
}
}
Return $ ret;
}
?>

Example Usage

// An image to convert
$ Image = 'test.jpg ';

// Do the conversion
$ Ascii = image2ascii ($ image );

// And show the world
Echo $ ascii;
?>


Http://www.bkjia.com/PHPjc/444992.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444992.htmlTechArticlephp image converted to ascii output image php image converted to ascii output image this PHP function creates a simple JPG image ASCII art rendering. It uses Guangdong for heavy work, and...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.