How to get text from image in php?

by Vincy. Last modified on July 8th, 2022.

Most of the image based operations can be performed by using PHP built-in functions. The image-based PHP functions are in GD library and we can use them for converting text into an image.

Before that, we must ensure that the GD library is enabled in the php.ini configuration. You can also run the PHP function phpinfo() to check if the GD is enabled.

View Demo

I submit a text input via a HTML form to the PHP code. In PHP, I invoke GD library functions to convert this text input into an image.

I create image layers for putting the text and a background. Then, I merged the layers and copy the required portion to show the final output image to the browser. If no text is entered, then it will be resolved with the JavaScript validation.

This screenshot shows the final output after converting the text input to an image. This example may be useful for creating custom captcha using PHP.

How to get text from image in php?

Get and Validate Text Input via HTML Form

This code shows the HTML form to get the text input from the user. On submitting this form, the text input will be validated using JavaScript and sent to the PHP. After processing image conversion, the output image will be displayed below this form

<form name="form" id="form" method="post" action="index.php"
        enctype="multipart/form-data" onsubmit="return validateForm();">
    <div class="form-row">

        <div>
            <label>Enter Text:</label> <input type="text"
                class="input-field" name="txt_input" maxlength="50">
        </div>
    </div>
    <div class="button-row">
        <input type="submit" id="submit" name="submit"
            value="Convert">
    </div>
</form>

Converting Text to an Image using PHP GD functions

In PHP, with the use of GD library function, we can convert text input to an image. I created a transparent image layer to place the text input onto it.

Then I created a background image layer and merge it together with the text input layer using imagecopymerge(). After merging, I slice the required part of the merged layer by using imagecopy() and output the final image to the browser.

<?php
if (isset($_POST['submit'])) {
    
    $img = imagecreate(500, 100);
    
    $textbgcolor = imagecolorallocate($img, 173, 230, 181);
    $textcolor = imagecolorallocate($img, 0, 192, 255);
    
    if ($_POST['txt_input'] != '') {
        $txt = $_POST['txt_input'];
        imagestring($img, 5, 5, 5, $txt, $textcolor);
        ob_start();
        imagepng($img);
        printf('<img src="data:image/png;base64,%s"/ width="100">', base64_encode(ob_get_clean()));
    }
}
?>

View DemoDownload

↑ Back to Top

How to get text from image in php?

We are now APPSeCONNECT. Welcome to the Future of Automation. Visit Now

Skip to content

  • CRM Services
    • SugarCRM Services
      • SugarCRM Customization
      • SugarCRM Developments
      • SugarCRM Integration
      • SugarCRM Migration Services
      • SugarCRM Upgrade Services
    • SuiteCRM Development Services
    • Small Business CRM
      • Top Affordable Small Business CRM
      • Small Business Owners guide to simple and intuitive CRM software 2017
    • CRM for Medical Device Distributors
  • SAP Consulting
    • SAP Salesforce Integration Services
    • SAP Bigcommerce Integration
    • SAP Magento Integration
    • SAP Integration Services
    • SAP SugarCRM Connector
  • Blog
  • Case Studies
    • Case Studies
    • Testimonials
  • Contact Now
  • Veon Blog | Useful tips and latest technology news
  • Contact Us

  • CRM Services
    • SugarCRM Services
      • SugarCRM Customization
      • SugarCRM Developments
      • SugarCRM Integration
      • SugarCRM Migration Services
      • SugarCRM Upgrade Services
    • SuiteCRM Development Services
    • Small Business CRM
      • Top Affordable Small Business CRM
      • Small Business Owners guide to simple and intuitive CRM software 2017
    • CRM for Medical Device Distributors
  • SAP Consulting
    • SAP Salesforce Integration Services
    • SAP Bigcommerce Integration
    • SAP Magento Integration
    • SAP Integration Services
    • SAP SugarCRM Connector
  • Blog
  • Case Studies
    • Case Studies
    • Testimonials
  • Contact Now

  • How to get text from image in php?

Get to know to use OCR within PHP to convert image to text

For various business reasons, you may want to convert an image file to text. Programs can be written to to achieve this using optical character recognition (OCR). Considering that many web applications are being written in PHP, lets take this opportunity to see how OCR can be leveraged within PHP to convert an image to text. Also refer to the article below to understand the use cases of image to text conversion.

Looking for a PHP development team? Reach out to us. 

If you want a little more background on how what optical character recognition is you can find this link useful. Tesseract is one of the popular OCR engine, which was developed by Hewlett Packard and now works with most of the platforms and various programming languages. Since it works very well with PHP, we will be using Tesseract for this example.


PHP OCR image to text conversion using Tesseract

In this section of the article, we will explore the image to text conversion in a step by step manner. For this the first step is to install Tesseract. You can find the steps to install the same below.

Get to know the top things you must consider before outsourcing PHP developments.

#1. Install Tesseract

The fist step in this exercise would be to install Tesseract. Following are the instructions on how the library can be installed in Ubuntu and macOS operating systems.

For Ubuntu 18.04 users

$ sudo apt install tesseract-ocr

For macOS Users

$ brew install tesseract –with-all-languages

#2. Add PHP library to interact with the OCR to the project

The next step is to enable/add the Tesseract library so that it is available for PHP scripts. Following is the command to do the same.

$ composer require thiagoalessio/tesseract_ocr

#3. Sample PHP code to invoke image to text conversion

Given below is a sample PHP code which takes the path of an image file from the file directory. It uploads the file before calling the Tesseract library and converts the same into text and outputs the same on the display.

You can find the code snippet on GitHub.

The following code is sample code to access the OCR engine to upload a image file from a directory and process it for text conversion.

				
					/*

 * If image file uploaded correctly then process image with the OCR

 */

if ($uploadOk) {

    $data = (new TesseractOCR($targetImageFile))

            ->lang('eng')

            ->run();

    file_put_contents('uploads/text.txt', $data);

}
				
			

Use case of image to text conversion

Following are some of the use cases for image to text conversion we have come across for parsing the image into text file.

CRM image upload

Many a times, sales representatives are in a hurry to gather information. Many a times, the information may be in a picture format. They would want it to be in a text format so that it can be used to update the respective customer record and be available for reporting.

Check out our CRM development service offerings.

Toll Tag

Many paid highways use OCR for reading the license plate of vehicles. Depending on the entry and exit points on the highway, the bill is generated and sent to the vehicle owner.

 Statement and journal conversion

For statements and journal entries which are available in picture and need to be entered into financial system, this can be done via OCR.

Coupon and Promotion via mobile APP picture data upload

Mobile APP users may want to capture pictures and get it converted to text before getting it uploaded to back end system. Information such as voucher and coupons can be scanned. Check out the popular OCR APP on Apple Store.

Fleet Tracking

Large fleet owners want to track entry and exit of cars into the parking. For this an OCR application can be used to read the movement of the cars.

Mobile Banking / Check upload 

If you are looking at building a banking application which can upload checks, you can use this concept to validate check information like amount, routing number and account number. Kindly note that this may require an additional layer of MICR instead of mere OCR.

From the above use cases, we can understand that OCR can tremendously improve user experience. We really hope that you learnt all about using image to text conversion using OCR in PHP. We wish you all the best for your next development project.


Evaluating SugarCRM for your business – We can help

Reach out to us so that we can assess and plan a road-map for your PHP development needs. Let’s build a system, which you will use for years to come.

How to get text from image in php?

Take your business to the next level

Schedule a free 15 minutes no obligation call with one of our experts. Change the way you interact with customers. 

Consult Now

By Divya Sahu|2021-04-06T10:33:25+00:00April 13th, 2020|CRM, SugarCRM, SuiteCRM|Comments Off on Get to know to use OCR within PHP to convert image to text

Divya is a CRM consultant at Veon Consulting. She helps companies derive the maximum benefit from applications such as SugarCRM and SuiteCRM. She also works on custom PHP developments. Her interests are interior decoration and travelling.

GDPR Acceptance
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

How to get text from image in php?

Veon is now APPSeCONNECT!

Welcome to the Future of Automation. Explore the modern integration platform to automate your business!