Like high-level languages such as Python,javascript, PHP can also write extension functions in C + +. Here's how to build a simple PHP extension and how to call a third-party DLL library.
Reference Original: Making PHP Barcode Extension with Dynamsoft Barcode SDK
Building PHP Extensions with Visual Studio 2012
The Windows PHP installation package itself does not contain the header files, so to build the PHP extension, you must download the PHP source code. On Windows, to compile PHP, and to build PHP extensions, you must use the corresponding visual Studio, otherwise there will be a lot of errors. Here we use Visual Studio 2012 to build a PHP 5.6 extension. The steps are as follows:
Download the source code for PHP 5.6 and the VC11 build version.
Create an empty Win32 project, and apply the type selection DLL.
To add a header file path:
F:\php_pack\php-5.6.10-srcf:\php_pack\php-5.6.10-src\zendf:\php_pack\php-5.6.10-src\win32f:\php_pack\ Php-5.6.10-src\tsrmf:\php_pack\php-5.6.10-src\main
To add a library path:
F:\php_pack\php-5.6.10-Win32-VC11-x86\dev
Add Dependencies:
Php5ts.lib
Create php_dbr.h:
#pragma once #include "zend_config.w32.h" #include "php.h"
-
Create php_dbr.cpp :
#include "Php_dbr.h" zend_function (decodebarcodefile); zend_function_entry customextmodule_ Functions[] = { zend_fe (decodebarcodefile, null) {null, null, null}}; zend_module_entry customextmodule_module_entry = { standard_module _header, "Dynamsoft barcode reader", customextmodule_ Functions, null, null, null, null, null, no_ Version_yet, standard_module_properties}; zend_get_module (customextmodule) ZEND_FUNCTION ( Decodebarcodefile) { return_string ("no barcode detected", true);}
To add a macro definition:
Zend_debug=0zts=1zend_win32php_win32
If you do not add it, many errors will occur.
The build project can now generate php_dbr.dll .
Use the Dynamsoft Barcode SDK to create PHP Barcode Extension
See how to call a third-party DLL library with PHP extensions:
Add Dynamsoft Barcode SDK header file and library file path to project properties
Decode the barcode from the SDK's C/C + + interface and convert the results to PHP readable data:
#include "Php_dbr.h" #include "If_dbr.h" #include "BarcodeFormat.h" #include " BarcodeStructs.h "#include ErrorCode.h" #ifdef _win64#pragma comment (lib, "DBRx64.lib ") #else #pragma comment (lib, " DBRx86.lib ") #endif void setoptions (preaderoptions Poption, int option_imaxbarcodesnumperpage, int option_llbarcodeformat) { if (option_llbarcodeformat > 0) poption->llbarcodeformat = option_llbarcodeformat; else poption->llbarcodeformat = oned; if (option_imaxbarcodesnumperpage > 0) poption- >iMaxBarcodesNumPerPage = option_iMaxBarcodesNumPerPage; else &Nbsp;poption->imaxbarcodesnumperpage = int_max; } zend_function (DecodeBarcodeFile); zend_function_entry customextmodule_functions[] = { zend_fe ( Decodebarcodefile, null) {null, null, null}}; zend_module_entry CustomExtModule_module_entry = { STANDARD_MODULE_HEADER, "Dynamsoft barcode reader", customextmodule_functions, null, null, null, null, null, no_version_yet, standard_module_properties}; zend_get_module (Customextmodule) zend_function (DecodeBarcodeFile) { array_init (Return_value); // get barcode image path char* pfilename = null; int ilen = 0; if (Zend_parse_parameters (Zend_num_args () TSRMLS_CC, "s", &pfilename, &ilen) == failure) { return_string (" Invalid parameters ", true); } // dynamsoft barcode reader: init int option_imaxbarcodesnumperpage = -1; int option_llBarcodeFormat = -1; pbarcoderesultarray presults = null; readeroptions option; setoptions (&option, option_imaxbarcodesnumperpage, option_ Llbarcodeformat); // decode barcode image file int ret = dbr_decodefile ( pfilename, &option, &pResults ); if (RET&NBSP;==&NBSP;DBR_OK) { int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; // loop all results for (int i = 0; i < count; i++) { tmp = Ppbarcodes[i]; // convert format type to string char format[64]; sprintf (format, "%d", tmp->llformat); // (Barcode type, result) Add_assoc_string (return_value, format, tmp->pbarcodedata, 1); } // Dynamsoft Barcode Reader: release memory dbr_freebarcoderesults (& Presults); } else { return_string ("no barcode detected", true); } }
Now we need to write a test script for PHP and deploy the DLL to PHP.
A simple PHP Barcode Reader:
<?php $filename = "F:\\git\\dynamsoft-barcode-reader\\images\\allsupportedbarcodetypes.tif"; if (file_exists ($filename)) {echo "Barcode file: $filename \ n"; $resultArray = Decodebarcodefile ($filename); if (Is_array ($resultArray)) {foreach ($resultArray as $key = = $value) {print "format: $key, result: $value \ n"; print "*******************\n"; }} else {print "$resultArray"; }} else {echo "The file $filename does not exist";}?>
Open the php.ini initialization file and add:
[Dynamsoft Barcode Reader]extension=php_dbr.dll
Now copy the generated DLL to {PHP root directory}\ext. If you also copy the DynamsoftBarcodeReaderx86.dll to this directory, PHP will not find this DLL, the following error is reported:
How do I fix this problem? You just have to copy the third-party DLL to the PHP root directory. Now try again:
Source
Https://github.com/yushulx/Dynamsoft-Barcode-Reader/tree/master/samples/PHP
Writing PHP Extension in C + +