How to Read Text from Image in PHP

Do you want a solution that reads text from an image in PHP? There are some cases where you want to read text programmatically. Probably you want to check whether text on an image is abusive, you may want to recognize the image from text, etc. In this article, we study how to read text from an image in PHP.

Tesseract OCR is an open source OCR engine which allows us to detect text in image. The user can install the Tesseract OCR engine on OS like Windows, macOS, Linux. You will get the installation instruction on their documentation.

Installation of Tesseract OCR Engine

As said above, Tesseract OCR is providing support to all major OS. For this tutorial, I assume you are using Windows OS. There are 2 ways to install Tesseract OCR on Windows machines. One is using the installer and another is using Chocolatey. The Chocolatey is a package manager for Windows and user-friendly for developers.

In the first way, Download the installer for Windows. Choose the installer for 32 bit or 64-bit system based on your machine configuration. Complete the installation process.

If you want to install it using Chocolatey, then simply run the command below in the Windows Powershell. Make sure you are running a command as an Administrator.

choco install capture2text --version 3.9

Once you installed Tesseract OCR on your Windows OS, set this path C:\Program Files\Tesseract-OCR in your environment variable. After setting the path in an Environment variable it is recommended to restart your system. Sometimes it does not take effect until you restart the system.

Next, install Tesseract OCR library in your PHP project. For this, run the command below from your project root directory.

composer require thiagoalessio/tesseract_ocr

Read Text from Image in PHP

You are ready with Tesseract OCR software and library. Now, you can easily detect text in the image. It just requires a few lines of PHP code. Let’s say you want to read the content of the below image.

Text

Place the image in the ‘images’ directory of your project. In the PHP file your code will be as follow:

<?php
require_once "vendor/autoload.php";

use thiagoalessio\TesseractOCR\TesseractOCR;

echo (new TesseractOCR('images/text.png'))
    ->run();

The final output you should get as follows:

The quick brown fox jumps over the lazy dog.

Using Tesseract OCR, you can read the text of various languages. All you need to do is download the required language file from this location. Let’s say you want to read text written in german language. Download the deu.traineddata file, and keep it inside C:/Program Files/Tesseract-OCR/tessdata.

After this, pass the ‘deu’ language in the code as shown below.

echo (new TesseractOCR('IMAGE_PATH'))
    ->lang('deu')
    ->run();

That’s it! It is that simple. I hope you got to know how to read text from the image in PHP. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *