DEV Community

Xiao Ling
Xiao Ling

Posted on • Originally published at

How to Build PHP Barcode QR Code Reading Extension on Windows and Linux

When using PHP, you may need to integrate a few C++ libraries sometimes. This article guides you through the process of building a PHP Barcode QR code reading extension with Dynamsoft C++ Barcode SDK on Windows and Linux.


Visual C++ Compiler

According to the PHP online tutorial, the supported Visual C++ compilers are as follows:

Visual C++ 14.0 (Visual Studio 2015) for PHP 7.0 or PHP 7.1.
Visual C++ 15.0 (Visual Studio 2017) for PHP 7.2, PHP 7.3 or PHP 7.4.
Visual C++ 16.0 (Visual Studio 2019) for master.
Enter fullscreen mode Exit fullscreen mode

If you only have Visual Studio 2022 on Windows, you must install the corresponding Visual C++ build tools by Visual Studio Installer.

visual studio build tools

Afterwards, find and run the vcvars64.bat file to set the environment variables.

visual c++ environment varaibles

For example

Here we use PHP 7.4, so we should run Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat via cmd.exe.

Note: It is important to use the matching Visual C++ compiler version. Otherwise, the generated extension cannot be loaded by PHP.

Create PHP Barcode QR Code Extension Project

We scaffold a PHP extension project with the following commands:

cd php-7.4.30-src/ext
php ext_skel.php --ext dbr
cd dbr
Enter fullscreen mode Exit fullscreen mode

Then copy DynamsoftCommon.h, DynamsoftBarcodeReader.h and DBRx64.lib to the project directory.

In dbr.c, we implement the extension functions DBRInitLicense() and DecodeBarcodeFile().

#include "DynamsoftBarcodeReader.h"

static void *hBarcode = NULL;

#define CHECK_DBR()                                         \
if (!hBarcode)                                              \
{                                                           \
    hBarcode = DBR_CreateInstance();                        \
    const char* versionInfo = DBR_GetVersion();             \
    printf("Dynamsoft Barcode Reader %s\n", versionInfo);   \


    char *pszLicense;
    size_t iLen;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &pszLicense, &iLen) == FAILURE)
        RETURN_STRING("Invalid parameters");
    char errorMsgBuffer[512];
    // Click to get a trial license.
    DBR_InitLicense(pszLicense, errorMsgBuffer, 512);
    printf("DBR_InitLicense: %s\n", errorMsgBuffer);



    // Get Barcode image path
    char *pFileName;
    long barcodeType = 0;
    size_t iLen;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &pFileName, &iLen, &barcodeType) == FAILURE)
        RETURN_STRING("Invalid parameters");

    if (hBarcode)
        int iMaxCount = 0x7FFFFFFF;
        TextResultArray *pResults = NULL;

        // Update DBR params
        PublicRuntimeSettings pSettings = {0};
        DBR_GetRuntimeSettings(hBarcode, &pSettings);
        pSettings.barcodeFormatIds = barcodeType;
        char szErrorMsgBuffer[256];
        DBR_UpdateRuntimeSettings(hBarcode, &pSettings, szErrorMsgBuffer, 256);

        // Barcode detection
        int ret = DBR_DecodeFile(hBarcode, pFileName, "");
        DBR_GetAllTextResults(hBarcode, &pResults);
        if (pResults)
            int count = pResults->resultsCount;
            int i = 0;
            char strLocalization[128];
            for (; i < count; i++)
                zval tmp_array;
                add_next_index_string(&tmp_array, pResults->results[i]->barcodeFormatString);
                add_next_index_string(&tmp_array, pResults->results[i]->barcodeText);
                add_next_index_stringl(&tmp_array, pResults->results[i]->barcodeBytes, pResults->results[i]->barcodeBytesLength);

                memset(strLocalization, 0, 128);
                sprintf(strLocalization, "[(%d,%d),(%d,%d),(%d,%d),(%d,%d)]", \
                pResults->results[i]->localizationResult->x1, pResults->results[i]->localizationResult->y1, \
                pResults->results[i]->localizationResult->x2, pResults->results[i]->localizationResult->y2, \
                pResults->results[i]->localizationResult->x3, pResults->results[i]->localizationResult->y3, \
                pResults->results[i]->localizationResult->x4, pResults->results[i]->localizationResult->y4); 
                add_next_index_string(&tmp_array, strLocalization);

                add_next_index_zval(return_value, &tmp_array);

static const zend_function_entry dbr_functions[] = {
    PHP_FE(DBRInitLicense, NULL)
    PHP_FE(DecodeBarcodeFile, NULL)
Enter fullscreen mode Exit fullscreen mode

config.w32 for Windows

To link DBRx64.lib, we specify LDFLAGS with linker options in config.w32 file.

ARG_ENABLE('dbr', 'dbr support', 'no');

if (PHP_DBR != 'no') {
    lib_path = "ext\\dbr";

    ADD_FLAG("LDFLAGS", '/libpath:"' + lib_path + '" /DYNAMICBASE "DBRx64.lib"');

    AC_DEFINE('HAVE_DBR', 1, 'dbr support enabled');

    EXTENSION('dbr', 'dbr.c', null, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
Enter fullscreen mode Exit fullscreen mode

config.m4 for Linux

To link shared libraries on Linux, we edit the config.m4 file.

  [whether to enable dbr support],
    [Enable dbr support])],

if test "$PHP_DBR" != "no"; then

  AC_MSG_ERROR([wrong dbr lib version or lib not found])


  AC_DEFINE(HAVE_DBR, 1, [ Have dbr support ])

  PHP_NEW_EXTENSION(dbr, dbr.c, $ext_shared)
Enter fullscreen mode Exit fullscreen mode

Then copy,, to the /usr/bin directory.

Steps to Build and Install the PHP Extension

There are two ways to build the extension. One is to use the source code, and the other is to use the pre-built binaries.

Build the PHP Extension from Source Code


cd php-7.4.30-src
configure --disable-all --enable-cli --enable-dbr
Enter fullscreen mode Exit fullscreen mode


cd php-7.4.30-src
./configure --disable-all --enable-cli --enable-dbr
Enter fullscreen mode Exit fullscreen mode

By default, the extension is statically linked. To build shared libraries, we can modify the line:

- configure --disable-all --enable-cli --enable-dbr
+ configure --disable-all --enable-cli --enable-dbr=shared
Enter fullscreen mode Exit fullscreen mode

Build the PHP Extension with phpize


cd php-7.4.30-src/ext/dbr
configure --enable-dbr
Enter fullscreen mode Exit fullscreen mode


cd php-7.4.30-src/ext/dbr
./configure --enable-dbr
Enter fullscreen mode Exit fullscreen mode

Install the PHP Extension with *.dll and *.so Files


  1. Add extension=dbr to php.ini.
  2. Copy the generated php_dbr.dll to the php/ext/ folder, and copy DynamicPdfx64.dll, DynamsoftBarcodeReaderx64.dll, DynamsoftLicenseClientx64.dll and vcomp110.dll to the PHP root directory.

    install PHP barcode Qr code extension on Windows


  1. Add extension=dbr to /etc/php/7.4/cli/php.ini.
  2. Run make install.

Test the PHP Barcode QR Code Reader Extension

  1. Apply for a 30-day FREE trial license.
  2. Create a PHP script reader.php that reads the barcode and QR code from a local image.

    $filename = "AllSupportedBarcodeTypes.tif";
    if (file_exists($filename)) {
        echo "Barcode file: $filename \n";
        // Get license key from
        //Best coverage settings
        $resultArray = DecodeBarcodeFile($filename, 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000); // 1D, PDF417, QRCODE, DataMatrix, Aztec Code
        if (is_array($resultArray)) {
            $resultCount = count($resultArray);
            echo "Total count: $resultCount\n";
            for ($i = 0; $i < $resultCount; $i++) {
                $result = $resultArray[$i];
                echo "Barcode format: $result[0], ";
                echo "value: $result[1], ";
                echo "raw: ", bin2hex($result[2]), "\n";
                echo "Localization : ", $result[3], "\n";
        } else {
            echo "$resultArray[0]";
    } else {
        echo "The file $filename does not exist";
  3. Run the PHP script in the terminal.

    php reader.php

    PHP barcode Qr code reader extension

Source Code

Top comments (0)