On this Blog you can read and learn to develop or create web server architectures

Enjoy and be free to write to me and get additional information or help about the tutorials


How to extract text from image or photo with Amazon Textract and Php

In this tutorial you can extract text from any image or photo from your phone as an OCR.   For this case we will use the Aws service called "TextRact".

OCR stands for Optical Character Recognition. It is a widespread technology to recognize text inside images, such as scanned documents and photos. OCR technology is used to convert virtually any kind of image containing written text (typed, handwritten, or printed) into machine-readable text data.

What is AWS TextRact?

Automatically extract printed text, handwriting, and data from any document.

Extract text and structured data such as tables and forms from documents using artificial intelligence (AI)—no configuration or templates necessary.

How it works

Amazon Textract is a machine learning (ML) service that automatically extracts text, handwriting, and data from scanned documents. It goes beyond simple optical character recognition (OCR) to identify, understand, and extract data from forms and tables. Today, many companies manually extract data from scanned documents such as PDFs, images, tables, and forms, or through simple OCR software that requires manual configuration (which often must be updated when the form changes). To overcome these manual and expensive processes, Textract uses ML to read and process any type of document, accurately extracting text, handwriting, tables, and other data with no manual effort. You can quickly automate document processing and act on the information extracted, whether you’re automating loans processing or extracting information from invoices and receipts. Textract can extract the data in minutes instead of hours or days. Additionally, you can add human reviews with Amazon Augmented AI to provide oversight of your models and check sensitive data.


Aws free or active Account, EC2 instance Linux Ubuntu Apache, IAM configuration, Linux console access, PHP7, PHP AWS SDK, Optional Mysql Rds Database.


First you need to download the official SDK for PHP to access to all services of AWS, in this case "TextRact".  To download the official SDK please Click Here

You can install the SDK via composer or uploading the aws folder to your root application.

For composer write on Linux console on your root project: 

composer require aws/aws-sdk-php

*Note: on this example we will use the normal installation on a root directory of the demo app.

If you don´t use composser installation, you need to call the service as follow:


You need to create an IAM user with all access to the features of Textract, please read the official documentation to configure your IAM user on AWS IAM or contact to me to get dedicated information about this feature.  When you generate the Access Keys credentials to access to all the features of Textract you will be abbly to use this demo app.

Please use this image guide to configurate your own IAM profile.  Note that i call my own permission as "textract" and give "Full Access" to Textract service.


First you need to create a directory on root folder called "uploads".  Please change the group permissions to "www-data" on  the linux console use the next command:

sudo chown -R www-data uploads

Create a new php file on your root directory of the project and run the demo sample.  This example has a html form and php code to process the upload file and show the text recognition on screen:

It´s very important that you configure correctly your credentials, please be carefull with the lines 35 - 42:

Region: use your region used for your instance.

Version: 2018-06-27


     Key: Your Own key generated for IAM user role

     Secret: Your own key secret generated for IAM user role.

Then you need to add the html code with the form to upload image or photo on computer or phone.

You can use your own styles, please copy the form exactly as the sample to get the same result.  

The next images shows how to work the demo on mobile device, upload your php file to your root directory project and run the file on your navigator:

Now please click on "Select file" and your mobile open the camera and select any object that has text to convert:

Take the photo and click on ✔️ icon.

Finally click on "Upload" button of the form and wait to get the text result converted:


Please contact to me if you have problems on the process.  This is only a sample, there are many options to convert PDF files or detect columns and rows in a document to convert in plain format text recognizing head names and more lables.  This applications uses S3 Services, PHP and Python codes.






Invite me a coffee! be free to support my tutorials and offer to you more tutorials about web development. Thanks a lot!

Duis ac pretium nisl?

Integer ac lorem est. Donec placerat sem eu ullamcorper dapibus.