
- Drop me a line! Contact me here.
Images to HTML in GD
Images to HTML in GD
One of the more pointless ideas around the internet is this process of creating an ascii art image from an image file such as a JPG. There is little of worth in these works, they're very little use to anyone, they take up more space than the original artwork, and quite frankly they're a total waste of time.
Which, of course, is why we need a script to generate them.
The first stage of converting an image to text is to open it and set up a loop to scan through it pixel by pixel. We will be converting each pixel to an ascii character.
The process of getting each pixels colour is straightforward and is the same as other scripts that get pixel colours.
This method of extracting the RGB colours relies on GD opening a truecolour image. If you're using an image with an indexed colour palette you would need to use a slightly different block of code. The way a colour palette works is by recording each colour in the image in a table, and then storing a reference code for each pixel to define what colour it should be.
We then store the colour of the pixel in an array as a hex colour to make the drawing process easier.
The final stage of the process is to loop through the $grid array and draw the ascii art output.
The code only echos a colour tag when one is necessary, rather than one for every character. It would be relatively simple to improve the code to use a different letter for each colour in the image.
Which, of course, is why we need a script to generate them.
<?php
$image = imagecreatefromjpeg("pic.jpg");
for ($y=0;$y<imagesy($image);$y++) {
for ($x=0;$x<imagesx($image);$x++) {
//Do stuff
}
}
?>
The first stage of converting an image to text is to open it and set up a loop to scan through it pixel by pixel. We will be converting each pixel to an ascii character.
The process of getting each pixels colour is straightforward and is the same as other scripts that get pixel colours.
<?php
$rgb = imagecolorat($image,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
?>
This method of extracting the RGB colours relies on GD opening a truecolour image. If you're using an image with an indexed colour palette you would need to use a slightly different block of code. The way a colour palette works is by recording each colour in the image in a table, and then storing a reference code for each pixel to define what colour it should be.
<?php
$color = imagecolorat($image,$x,$y);
$rgb = imagecolorsforindex($image, $color);
$r = $rgb['red'];
$g = $rgb['green'];
$b = $rgb['blue'];
?>
We then store the colour of the pixel in an array as a hex colour to make the drawing process easier.
<?php
$grid[$x][$y] = 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);
?>
The final stage of the process is to loop through the $grid array and draw the ascii art output.
<?php
for ($y=0;$y<imagesy($image);$y++) {
for ($x=0;$x<imagesx($image);$x++) {
if ($grid[$x][$y] != $pHex) {
if ($pHex != "") {
echo "</span>";
}
echo "<span style=\"color:".$grid[$x][$y]."\">";
$pHex = $grid[$x][$y];
}
echo "#";
}
echo "<br>\n";
}
?>
The code only echos a colour tag when one is necessary, rather than one for every character. It would be relatively simple to improve the code to use a different letter for each colour in the image.
Comments
Comments are not currently being accepted for this article.
Sidebar
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 7
- DB Time: 0.1193 seconds
