Convert Image To Text Using PHP Script

Published on Author Code Father
Convert Image To Text Using PHP Script. 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.

<?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>’;

}

?>

Comments

comments

Categories PHP