Convert Image In To Text Using PHP Script

Posted by in Articles, PHP

Ever wanted to create those text made roses and heart shapes that you see in Facebook and Myspace? You might be wondering how they create such. However there are some websites that does the job, you may still be curios to do it by yourself. They convert your image file in to a text which exactly renders as an image. If you are still unclear what I am talking about then see how this JPEG image is manipulated to text.



First view the demo here.

You might be surprised that this manipulation can be done just by PHP code not more than 30 lines.
We’ll do this just by a PHP code.

Let me show you how can you use PHP imagecreatefromjpeg() function to manipulate the pixeleted image in ASCII character form.
First of all let’s summarize what we’ll do.

First, we’ll create a variable pointing the location of the image. Now we’ll feed that image location variable to imagecreatefromjpeg() function like this:

$locate= 'http://website.com/image.jpg';
$image = imagecreatefromjpeg("$locate");

Now the imagecreatefromjpeg() function manipulates the image details like RGB color coding, white pixels etc.

Since our PHP code have the details of the image, we’ll add conditional tag what to do next if the image have been manipulated by the imagecreatefromjpeg() function. In the conditional statement we’ll fetch the RGB value, get the brightness situation and finally put a ASCII character along with the brightness.

What our code needs to do is loop through each row in the image, and within each row it needs to also loop through each column. This means we’re accessing each pixel one-by-one, which is what we need. Then, for each pixel in the image, we need to grab the red, green, and blue color values of the pixel, add them all together.

By default, we’ll define the ASCII keys as array for the brightness. For example, [@#+*;:, ] we’ve increasing brightness symbol from @ which denotes the extreme dark and a white space at last which denote extreme white.

The darker part will be replaced by @ and white part will be replaced by white space, while other characters will denote the level of brightness in eight characters.

Have a look at the PHP code below:

echo '<PRE STYLE="font: 1px/1px Courier New;">';

$asciichars = array("@", "#", "+", "*", ";", ":", ",", ".", "`", " ");

$width = imagesx($image);

$height = imagesy($image);

for($y = 0; $y < $height; ++$y) {

for($x = 0; $x < $width; ++$x) {

$thiscol = imagecolorat($image, $x, $y);

$rgb = imagecolorsforindex($image, $thiscol);

$brightness = $rgb['red'] + $rgb['green'] + $rgb['blue'];

$brightness = round($brightness / 85);

$char = $asciichars[$brightness];

echo $char;

}

echo "\n";

}
echo '</PRE>';

}

And finally we’ll print the result.

The final code will be like this:

<?php

$locate= ‘http://website.com/image.jpg’;

$image = imagecreatefromjpeg("$locate");

if ($image) {

echo '<PRE STYLE="font: 1px/1px Courier New;">';

$asciichars = array("@", "#", "+", "*", ";", ":", ",", ".", "`", " ");

$width = imagesx($image);

$height = imagesy($image);

for($y = 0; $y < $height; ++$y) {

for($x = 0; $x < $width; ++$x) {

$thiscol = imagecolorat($image, $x, $y);

$rgb = imagecolorsforindex($image, $thiscol);

$brightness = $rgb['red'] + $rgb['green'] + $rgb['blue'];

$brightness = round($brightness / 85);

$char = $asciichars[$brightness];

echo $char;

}

echo "\n";

}

echo '</PRE>';

}

?>

Just copy it to your notepad and save it as image.php. Before saving define the location of picture.
You can also create a form to specify the location of the image and input the form value in $locate variable.
Posibilities are endless, who knows one can start his own website only with this script.
Again, view the demo here.