How to Build PHP Barcode Reader on Linux

No matter whether you are a web developer or an end-user, you probably have heard of the terms SaaS, Paas, and IaaS. They are three different models of cloud service. Why more and more people tend to embrace web and cloud services such as Google Cloud, Azure, and AWS? Because no application download and installation needed. A variety of software and functions can be accessed via web browsers. Assuming you want to build a web barcode reader as a service from scratch. Using Dynamsoft Barcode Reader SDK will expedite your development.

This article has been posted on CodeProject.

linux php barcode reader

SDK Download

Dynamsoft Barcode Reader 30-day Free Trial

Server-side PHP Barcode Reader

Dynamsoft Barcode Reader supports 64-bit PHP from 5.3 to 5.6. Therefore, you need to firstly check the version of PHP that installed on your system:

php -v

In addition to PHP version, you need to know whether the PHP is thread-safe or not:

 php –i | grep Thread

Copy shared libraries of Dynamsoft Barcode Reader to /usr/lib:

sudo cp <Dynamsoft Barcode Reader>/lib/* /usr/lib

Edit php.ini to add following extension path:

extension=<Dynamsoft Barcode Reader>/php/extension/<version>/<ts|nts>/php_DynamsoftBarcodeReader.so

Upload barcode images to a web server with Form:

<form id="uploadForm" method="post" action="readbarcode.php" enctype="multipart/form-data">
    <input type="file" id="upLoadFile" name="upLoadFile" class="ImgLocalPath" />
    <input type="text" readonly="readonly" id="txtUploadFileName" class="radius3" />
    <input type="button" id="btnUploadFile" value="Browse..." class="radius3 ml20"/>
    <input type="submit" id="btnReadBarcode" class="radius3 left ml20"  value="Read Barcode" />

</form>

Detect barcode data on server-side:

<?php
include 'DynamsoftBarcodeReader.php';
ini_set('display_errors',1);
error_reporting(E_ALL);
$post_max_size = ini_get("post_max_size");
$maxsize = return_bytes($post_max_size);

if($_SERVER['CONTENT_LENGTH'] > $maxsize) {
         echo "Post data size is bigger than " . $post_max_size;
         exit;
}

$file = $_FILES["upLoadFile"]["tmp_name"];
if(!empty($file)){
         readBarcode($file);                                         
}
else {
         echo "Fail to upload file.";
}

function readBarcode($path) {
    try {       
        $br = new BarcodeReader();
    } catch (exception $exp) {       
        echo 'Your barcode reader component is not registered correctly. Please refer to ReadMe.txt for details.<br/>';
        exit;
    }

    $br->initLicense('693C401F1CC972A5018B729568B0CDD8');

    try {       
        $br->decodeFile($path);
    } catch(Exception $exp) {
        echo $br->getErrorString() . '<br/>';
        exit;
    }

    $cnt = $br->getBarcodesCount();
    if($cnt > 0) {
        echo 'Total barcode(s) found:' . $cnt . '.<br/>';
        for ($i = 0; $i < $cnt; $i++) {
            $result = $br->getBarcodeResult($i);
             echo ($i+1) . ': ';
             echo "$result->BarcodeFormatString, ";
             echo "$result->BarcodeText<br/>";
        }
    }
    else {
        echo 'No barcodes found.<br/>';
    }   
}
?>

How to Deploy the Sample Code to Apache on Ubuntu

Install php5-curl, apache2 and libapache2-mod-php5:

sudo apt-get install php5-curl apache2 libapache2-mod-php5

Extract the package and copy the project to /var/www/html/:

sudo cp -r DecodeLocalFile /var/www/html

Add extension path to /etc/php5/apache2/php.ini.

Start Apache service:

sudo service apache2 start

Visit http://localhost/DecodeLocalFile/index.php in Firefox or Chrome.

Online Demo & Source Code

Visit PHP Barcode Reader to experience the online demo.

Download the source code.