PHP Script To Turn Image Into ASCII Text

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a "span" element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a   character instead.

$img = ImageCreateFromJpeg('logo.jpg');

// Get height and width of the image.
$width = imagesx($img);
$height = imagesy($img);

echo '<pre style="font-size:1px;">';

for ($h=0; $h < $height; $h++) {
 for ($w=0; $w <= $width; $w++) {
  if ($w == $width) {
   echo "\n";
   continue;
  }
  // Get colour at pixel location.
  $rgb = ImageColorAt($img, $w, $h);

  // Convert colour into usable format.
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8 ) & 0xFF;
  $b = $rgb & 0xFF;

  // Check for white/off-white colour.
  if ($r > 200 && $g > 200 && $b > 200) {
   echo '&nbsp;';
  } else {
   echo '<span style="color:rgb(' . $r . ',' . $g . ',' . $b . ');">#</span>';
  }
 }
}
echo '</pre>';

As an example, take the following, quite recognisable, image.

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

This is transformed into the text on the following page.

The Google logo in ASCII format

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

Comments

Is there a way to convert image (there are bunch of texts on it) to text via PHP?
Permalink
You need some form of OCR library. The last time I looked there wasn't a good PHP library to do this, but things may have progressed.
Name
Philip Norton
Permalink
I have to say I am very impressed with the code. I have seen and tried ascii art generator php code from other sources that are WAAAAY long and don't work. This in awesome.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
15 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.