Home Tutorial Php Phpgd PHP GD image to text

 
 

PHP GD image to text
Posted on: November 3, 2009 at 12:00 AM
This example shows how to change image to text in php gd.

<?php

$file = imagecreatefromjpeg("image1.jpg");

$bw = 0;

$s = "index";

$dest_x = 40;

$dest_y = 30;

if (imagesx($file) > $dest_x or imagesy($file) > $dest_y) {

if (imagesx($file) >= imagesy($file)) {

$full_y = imagesy($file)*($dest_x/imagesx($file));

$full_x = $dest_x;

} else {

$full_x = imagesx($file)*($dest_y/imagesy($file));

$full_y = $dest_y;

}

} else {

$full_x = imagesx($file);

$full_y = imagesy($file);

}

$image = imagecreatetruecolor($full_x, $full_y);

imagecopyresized($image, $file, 0, 0, 0, 0, imagesx($image), imagesy($image), imagesx($file), imagesy($file));

for ($y=0;$y<imagesy($image);$y++) {

for ($x=0;$x<imagesx($image);$x++) {

 

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

$r = ($rgb >> 16) & 0xFF;

$g = ($rgb >> 8) & 0xFF;

$b = $rgb & 0xFF;

 

if ($bw == 1) {

$max = max($r,$g,$b);

$grid[$x][$y] = str_repeat(str_pad(dechex($max),2,"0",STR_PAD_LEFT),3);

} else {

$grid[$x][$y] = sprintf("%02X%02X%02X",$r,$g,$b);

}

}

}

$pHex = "";

echo "<div style=\"line-height:0.8em; font-size:8pt; font-family:monospace; background-color: #000000;\">";

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 $s[($counter++%strlen($s))];

}

echo "<br>\n";

}

echo "</div>";

?>

After running the program you will get the following output

Related Tags for PHP GD image to text:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.