RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Image To Ascii Art With PHP

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

1
<?php 
2
/*
3
 Image To Ascii Art With PHP
4
 http://www.beliefmedia.com/code/php-snippets/ascii-art
5
*/
6
 
7
function beliefmedia_asciiart($image) {
8
 
9
  $image = file_get_contents($image);
10
  $img = imagecreatefromstring($image);
11
  $width = imagesx($img);
12
  $height = imagesy($img);
13
 
14
  for ($h=0; $h<$height; $h++) {
15
   for ($w=0; $w<=$width; $w++) {
16
    $rgb = imagecolorat($img, $w, $h);
17
    $a = ($rgb >> 24) & 0xFF;
18
    $r = ($rgb >> 16) & 0xFF;
19
    $g = ($rgb >> 8) & 0xFF;
20
    $b = $rgb & 0xFF;
21
    $a = abs(($a / 127) - 1);
22
 
23
      if ($w == $width) {
24
       $ascii .= '';
25
        } else {
26
       $ascii .= '<span style="line-height:0.3; font-size:1px; color:rgba('.$r.','.$g.','.$b.','.$a.');">#</span>';
27
      }
28
    }
29
  }
30
 return $ascii;
31
}
32
 
33
/* Usage */
34
echo beliefmedia_asciiart($image = 'info.jpg');

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:

1
<style>
2
 body {
3
  line-height:0.3;
4
  font-size:1px;
5
 }
6
</style>

Considerations

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 (300x300) sample image rendered a 6.7MB HTML page.

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

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment