对比windows,linux上实现php扩展要简单轻松的多。
参考原文:how to make php barcode reader on linux
作者:xiao ling
翻译:yushulx
几步构建php barcode扩展 安装dbr。
要构建php扩展,一定要使用对应版本的源码:
php –v
下载php源码。
解压代码,切换到ext目录:
cd ~/downloads/php-5.5.9/ext
创建扩展dbr:
./ext_skel --extname=dbrcd dbr
编辑config.m4,添加头文件和库文件路径:
php_arg_enable(dbr, whether to enable dbr support, dnl make sure that the comment is aligned: [ --enable-dbr enable dbr support]) if test $php_dbr != no; then php_add_library_with_path(dynamsoftbarcodereaderx64, /home/xiao/dynamsoft/barcodereader4.0/redist, dbr_shared_libadd) php_add_include(/home/xiao/dynamsoft/barcodereader4.0/include) php_subst(dbr_shared_libadd) php_new_extension(dbr, dbr.c, $ext_shared) fi
编辑dbr.c:
#ifdef have_config_h#include config.h#endif #include php.h#include php_ini.h#include ext/standard/info.h#include php_dbr.h #include if_dbr.h#include barcodeformat.h#include barcodestructs.h#include errorcode.h#include /* if you declare any globals in php_dbr.h uncomment this:zend_declare_module_globals(dbr)*/ /* true global resources - no need for thread safety here */static int le_dbr; /* {{{ dbr_functions[] * * every user visible function must have an entry in dbr_functions[]. */const zend_function_entry dbr_functions[] = { php_fe(decodebarcodefile, null) /* for testing, remove later. */ php_fe_end /* must be the last line in dbr_functions[] */};/* }}} */ /* {{{ dbr_module_entry */zend_module_entry dbr_module_entry = {#if zend_module_api_no >= 20010901 standard_module_header,#endif dbr, dbr_functions, php_minit(dbr), php_mshutdown(dbr), php_rinit(dbr), /* replace with null if there's nothing to do at request start */ php_rshutdown(dbr), /* replace with null if there's nothing to do at request end */ php_minfo(dbr),#if zend_module_api_no >= 20010901 php_dbr_version,#endif standard_module_properties};/* }}} */ #ifdef compile_dl_dbrzend_get_module(dbr)#endif /* {{{ php_ini *//* remove comments and fill if you need to have entries in php.iniphp_ini_begin() std_php_ini_entry(dbr.global_value, 42, php_ini_all, onupdatelong, global_value, zend_dbr_globals, dbr_globals) std_php_ini_entry(dbr.global_string, foobar, php_ini_all, onupdatestring, global_string, zend_dbr_globals, dbr_globals)php_ini_end()*//* }}} */ /* {{{ php_dbr_init_globals *//* uncomment this function if you have ini entriesstatic void php_dbr_init_globals(zend_dbr_globals *dbr_globals){ dbr_globals->global_value = 0; dbr_globals->global_string = null;}*//* }}} */ /* {{{ php_minit_function */php_minit_function(dbr){ /* if you have ini entries, uncomment these lines register_ini_entries(); */ return success;}/* }}} */ /* {{{ php_mshutdown_function */php_mshutdown_function(dbr){ /* uncomment this line if you have ini entries unregister_ini_entries(); */ return success;}/* }}} */ /* remove if there's nothing to do at request start *//* {{{ php_rinit_function */php_rinit_function(dbr){ return success;}/* }}} */ /* remove if there's nothing to do at request end *//* {{{ php_rshutdown_function */php_rshutdown_function(dbr){ return success;}/* }}} */ /* {{{ php_minfo_function */php_minfo_function(dbr){ php_info_print_table_start(); php_info_print_table_header(2, dbr support, enabled); php_info_print_table_end(); /* remove comments if you have entries in php.ini display_ini_entries(); */}/* }}} */ // barcode formatconst char * getformatstr(__int64 format){ if (format == code_39) return code_39; if (format == code_128) return code_128; if (format == code_93) return code_93; if (format == codabar) return codabar; if (format == itf) return itf; if (format == upc_a) return upc_a; if (format == upc_e) return upc_e; if (format == ean_13) return ean_13; if (format == ean_8) return ean_8; if (format == industrial_25) return industrial_25; if (format == qr_code) return qr_code; if (format == pdf417) return pdf417; if (format == datamatrix) return datamatrix; return unknown;} php_function(decodebarcodefile){ array_init(return_value); // get barcode image path char* pfilename = null; bool isnativeouput = false; bool islogon = false; int ilen = 0; if (zend_parse_parameters(zend_num_args() tsrmls_cc, sb|b, &pfilename, &ilen, &isnativeouput, &islogon) == failure) { return_string(invalid parameters, true); } if (islogon) { printf(params: %s, %d, %d\n, pfilename, ilen, isnativeouput); } // dynamsoft barcode reader: init __int64 llformat = (oned | qr_code | pdf417 | datamatrix); int imaxcount = 0x7fffffff; int iindex = 0; readeroptions ro = {0}; pbarcoderesultarray presults = null; int iret = -1; char * psztemp = null; // initialize license iret = dbr_initlicense(84d34246fc1bc4bdd4078d71fcb5a3aa); printf(dbr_initlicense ret: %d\n, iret); ro.llbarcodeformat = llformat; ro.imaxbarcodesnumperpage = imaxcount; // decode barcode image int ret = dbr_decodefile(pfilename, &ro, &presults); if (ret == dbr_ok) { int count = presults->ibarcodecount; pbarcoderesult* ppbarcodes = presults->ppbarcodes; pbarcoderesult tmp = null; char result[2048] = {0}; int i = 0; if (count == 0) { add_next_index_string(return_value, no barcode detected, true); } // loop all results for (; i llformat), true); add_next_index_string(tmp_array, tmp->pbarcodedata, true); // add result to returned array add_next_index_zval(return_value, tmp_array); } } // dynamsoft barcode reader: release memory dbr_freebarcoderesults(&presults); if (islogon && isnativeouput) { printf(native result: %s\n, result); } if (isnativeouput) { add_next_index_string(return_value, result, true); } } else { add_next_index_string(return_value, no barcode detected, true); } }
要独立构建php扩展模块,需要使用phpize。安装开发包:
sudo apt-get install php5-dev
构建php扩展模块:
phpize./configuremake
c90规范不支持布尔类型。如果看到错误“unknown type name ‘bool’”,加上头文件:
#include
动态链接库dbr.so会自动生成到目录modules下面:
简单的php barcode应用 创建reader.php:
安装刚才编译出来的扩展:
sudo make install
把安装之后的路径(/usr/lib/php5/20121212/dbr.so)加入到php.ini文件中:
locate php.ini
通过命令找寻php.ini文件会发现有好几个相关的。到底用哪个呢?可以随便选一个或者新建一个。我选择了php.ini-production :
extension=/usr/lib/php5/20121212/dbr.so
运行php barcode reader,记得带上php.ini的文件路径:
php -c /usr/share/php5/php.ini-production reader.php
源码 https://github.com/dynamsoftlabs/linux-php-barcode-reader-