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

Our Features

Dynamsoft's Barcode Reader SDK is implemented in C/C++, and therefore, it is convenient to build PHP extensions for Windows, Linux, and macOS. On Windows, Dynamsoft provides ActiveX/COM technology for expediting PHP development.

Here is an online demo showing how to use COM of Dynamsoft Barcode Reader SDK in PHP. It supports reading barcodes from images (PNG, TIFF, JPEG, BMP and more) and PDF files.

Barcode Symbologies Support

  • Linear Barcodes (1D):
    Code 39 (including Code 39 Extended), Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC-A, UPC-E, Industrial 2 of 5
  • 2D Barcodes:
    QR Code (including Micro QR Code), Data Matrix, PDF417 (including Micro PDF417), Aztec Code, MaxiCode (mode 2-5), DotCode
  • Patch Code
  • GS1 Composite Code
  • GS1 DataBar (Omnidirectional, Truncated, Stacked, Stacked Omnidirectional, Limited, Expanded, Expanded Stacked)
  • Postal Codes:
    USPS Intelligent Mail, Postnet, Planet, Australian Post, UK Royal Mail

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.


PHP web development consists of front-end and back-end. In addition to decoding barcodes on the server-side using PHP, developers can use the JavaScript barcode SDK provided by Dynamsoft for front-end development as well.

Learn more: JavaScript API for barcode scanning ›