Press enter to see results or esc to cancel.

Convert Image to Text Optical Character Recognition OCR Using PHP

In this video I have shown how you can convert image to text or do Optical Character Recognition OCR using PHP as front end. You can upload the file you want to do the OCR and the result will show on the page itself.

First you have to download the tesseract application and install. You can download it from below link.

You can download that in the below link.
Download tesseract-ocr-setup files here

After installing the OCR Application in any location, In my PC its installed in “C:\Program Files (x86)\Tesseract-OCR”.



<h3>PHP OCR Test</h3>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>


the form in index.php will submit the image to upload.php and will upload the file into the images folder in root directory.

$file_name = $_FILES['image']['name'];
$file_tmp =$_FILES['image']['tmp_name'];
echo "<h3>Image Upload Success</h3>";
echo '<img src="images/'.$file_name.'" style="width:100%">';

shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract" "C:\\xampp\\htdocs\\images\\'.$file_name.'" out');

echo "<br><h3>OCR after reading</h3><br><pre>";

$myfile = fopen("out.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("out.txt"));
echo "</pre>";

After the image is saved in images folder, The command prompt code in upload.php will process the CMD commands to do OCR on the page and create the output txt file in the root folder.
Later the out.txt is read and displayed in the page itself.

The test image file is,



root folder



Comments are disabled for this post