Use Dynamsoft Barcode Reader to Build a Barcode Scanner in PHP on Windows

Our Features

Dynamsoft's Barcode Reader SDK Windows Edition provides three APIs: C/C++, ActiveX/COM and .NET. You can enable barcode reading in your PHP web applications with the PHP wrapper using the ActiveX control.

Before providing more details, you may try the barcode PHP wrapper here. It supports reading barcodes from images (PNG, TIFF, JPEG, BMP and more) and PDF files.

Barcode Symbologies Support

Both Linear and 2D barcode types are supported:

  • 1D barcode support includes Code 39, Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC-A, UPC-E, Industrial 2 of 5;
  • 2D barcode support includes Data Matrix, QR Code, Aztec and PDF417.

Server Support

  • OS: Windows
    The backend of Dynamsoft's Barcode Reader is using an ActiveX port. Thus, for now, the wrapper only works on applications deployed on Windows. If your application happens to reside on Linux, you may take a look at our Linux + barcode solution.
  • IIS 7.5 and above
  • Apache 2.2 and above

PHP Support

  • PHP 5.5 and above

Build the Hello World Program

Prerequisite

Enable COM support in PHP

From PHP 5.4.5, COM and DOTNET is no longer built into the PHP core.

You have to add COM support by inserting this line in php.ini:
extension = php_com_dotnet.dll

Otherwise you will see an error in your error log:
Fatal error: Class 'COM' not found

Installation of Dynamsoft Barcode Reader

Download one of our PHP samples from Code Gallery

In the sample directory, you will find the following files:

  • {Sample Directory}\ActiveX\register.bat
  • {Sample Directory}\DynamsoftBarcodeReader.php

Please run the register.bat file as administrator to register the required barcode reader DLL.

Code Snippet

Step 1.

Suppose you are creating a project in a folder called test. In the folder, create a test.php file and a readbarcode.php file. Also, copy the aforementioned dynamsoftbarcodereader.php file from the SDK package to the test folder.

Edit the test.php as follows:

<html>
  <head />
  <body>
        <h1>Dynamsoft Barcode Reader PHP Simple Sample</h1>
        <form action="readbarcode.php" method="post" enctype="multipart/form-data">
            Select barcode image:
            <input type="file" name="barcodefile" id="barcodefile" accept="image/*" /><br/>
            <type="submit" value="Read Barcode" name="submit" />
    </form>
   </body>
</html>

Step 2.

Then edit the readbarcode.php file. Please update <your license key here> and <your image file here> with valid values in the code respectively. You may log in the customer portal and request for a trial extension online.

<?php
include "dynamsoftbarcodereader.php";

$uploadfile = $_FILES["barcodefile"]["tmp_name"];

$br = new BarcodeReader("<your license key here>");
$resultAry = $br->decodeFile("<your image file here>", "");

$cnt = count($resultAry);
if($cnt > 0){
    echo "Barcode Count: $cnt <br />";
    for ($i = 0; $i < $cnt; $i++) {
          $res = $resultAry[$i];
          echo "$i. $res->BarcodeFormatString , $res->BarcodeText<br />";
    }
}
?>

Step 3.

Visit http://[ip]:[port]/test/test.php to verify if it works.