Image to text in php

Do you want to know how to read text from an image in PHP? There are certain scenarios where you want to extract the text written on an image programmatically. Probably you want to check whether the text on an image is abusive, to recognize the image from text, etc. In this article, we study how to programmatically read text from an image in PHP.

Tesseract OCR is an open-source OCR engine that allows us to detect text in the image. The user can install the Tesseract OCR engine on OS like Windows, macOS, and Linux. You will get the installation instructions on their documentation. In this tutorial, I’ll explain the installation of Tesseract OCR on Linux and Windows machines.

The alternate ways of the reading text of the image are via Google Cloud Vision and Amazon Textract. Both are cloud services and do not require installing anything on your machine except their PHP library. Though it’s paid service if you want to give it a try follow the linked articles.

Installation of Tesseract OCR Engine on Windows

First, download the tesseract installer for Windows. Choose the installer for a 32-bit or 64-bit system based on your machine configuration. Complete the installation process.

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

With Tesseract OCR, you can read the text in 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 the German language. Download the deu.traineddata file from the linked page, and keep it inside C:/Program Files/Tesseract-OCR/tessdata.

Install Tesseract OCR on Linux

Using the apt command-line utility one can easily install the Tesseract OCR on the Linux distribution. The below command will install tesseract under the usr/share/tesseract-ocr/4.00/tessdata.

sudo apt install tesseract-ocr

This command will install the English language pack. To install the additional language, you need to pass the language code to the command below. Here, I am installing the German language which has a code deu.

sudo apt install tesseract-ocr-deu

Read Text from Image in PHP

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

composer require thiagoalessio/tesseract_ocr

You are ready with Tesseract OCR software and its 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.

Image to text in php

Place this image in the images directory of your project. To read the text of this image your PHP code will be as follow:

run();
} catch(Exception $e) {
    echo $e->getMessage();
}

The final output should be as follows:

The quick brown fox jumps over the lazy dog.

For reading the text written in another language, pass the language code to the lang() method 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

  • Speech-To-Text using Amazon Transcribe in PHP
  • PayPal Payment Gateway Integration in PHP using PayPal REST API
  • How to Detect Mobile Device in PHP

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

I would like to try to read some of text from an image with PHP.

So if I have an image like this:

Image to text in php

How can I extract the text "Some text" into a string.

All help and suggestions are appreciated.

h2ooooooo

38.2k8 gold badges65 silver badges101 bronze badges

asked Jan 7, 2010 at 22:43

AdnanAdnan

25.1k18 gold badges78 silver badges110 bronze badges

1

3 Answers

Sorted by: Reset to default

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Not the answer you're looking for? Browse other questions tagged php image or ask your own question.

How to extract text from image in PHP?

How to Read Text from Image in PHP.
Tesseract OCR is an open-source OCR engine that allows us to detect text in the image. The user can install the Tesseract OCR engine on OS like Windows, macOS, and Linux. ... .
Using the apt command-line utility one can easily install the Tesseract OCR on the Linux distribution. ... .
That's it!.

What is OCR in PHP?

Optical Character Recognition (OCR) is the process of taking image based versions of characters and converting them into machine encoded text. Some popular use cases include: Data entry for business documents, e.g. Cheque, passport, invoice, bank statement and receipt.

How to use Tesseract OCR in PHP?

php shell_exec('"C:\\Program Files\\Tesseract-OCR\\tesseract" "example_image. PNG" out'); ?> (Part 1) Here is the explanation to The first part of this code defines the directory where Tesseract is installed. So change the directory based on your computer file.

How to read image in PHP?

The Imagick::readImage() function is an inbuilt function in PHP which is used to read an image from filename. Parameters:This function accepts a single parameter $filename which holds the name of the file. It can also accept URL of the file. Return Value: This function returns TRUE on success.