Image To Ascii Art With PHP

and like us on FB

There are a bunch of Image to Ascii functions floating around the web, and there’s a number of excellent classes available if the function is required for anything other than the basic entertainment and educational value.

This is my version of the Image to Ascii art function. It’ll convert any GD supported image into ASCII art.

Consider the following image:

Image To Ascii Art With PHP

Using the function of echo beliefmedia_asciiart($image = 'info.jpg'); we’ll render the following as text (I’ve taken a screenshot of the result).

Image To Ascii Art With PHP

The PHP Code

If you wanted to produce a form or something similar where users could convert images, you might consider styling the body to minimise the size of the returned HTML. You could do so as follows:


The function literally replaces image pixels with individual RGB(A) values, so each pound symbol (representing each pixel) is styled and colourised individually. The function generates an enormous amount of HTML, and consumes quite a large amount of processing power. The larger the image, the more code will be returned, and the more CPU usage will be consumed. Our small (300×300) sample image rendered a 6.7MB HTML page.

Image To Ascii Art With PHP

Large amount of HTML code is generated.

If you had any need to actually keep a version of the generated code locally, you should choose to have it cached.