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


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

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

  • \Barcode Reader 5.1\Components\PHP\dynamsoftbarcodereader.php
  • \Barcode Reader 5.1\Components\ActiveX\DynamsoftBarcodeReaderCtrlx86.dll or \Barcode Reader 5.1\Components\ActiveX\DynamsoftBarcodeReaderCtrlx64.dll

Code Snippet

You can either download the source code here or follow the steps below to code from scratch.

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:

  <head />
        <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/>
            <input type="submit" value="Read Barcode" name="submit"/>

Step 2.

Then edit the readbarcode.php file.Please update <your license key here> with valid values in the code, respectively. The licenses can be found in the [INSTALL FOLDER]\LicenseManager.exe.

include "dynamsoftbarcodereader.php";

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

$br = new BarcodeReader();
$br->initLicense("<your license key here>");

$cnt = $br->getBarcodesCount();
echo "Barcode Count: $cnt <br />";
for ($i = 0; $i < $cnt; $i++) {
      $res = $br->getBarcodeResult($i);
      echo "$i. $res->BarcodeFormatString , $res->BarcodeText<br />";

Step 3.

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