PHP识别二维码的方法(php-zbarcode扩展)
源码来自github :https://github.com/mkoppanen/php-zbarcode
1.安装ImageMagick(http://www.imagemagick.org/)
yum install ImageMagick.x86_64 ImageMagick-devel.x86_64
wget http://jaist.dl.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.bz2 tar jxvf zbar-0.10.tar.bz2 cd zbar-0.10 #注意此步有大坑,要禁止gtk,python和qt的支持,不然你就等着无限报错吧 ./configure --without-gtk --without-python --without-qt --prefix=/usr/local/zbar
#./configure --without-gtk --disable-video --without-imagemagick --without-python --without-qt --without-gtk --without-x --prefix=/usr/local/zbar 还要禁止video
make && make install #提示如下为完成,不是报错 #make[2]: Leaving directory `/root/zbar-0.10\' #make[1]: Leaving directory `/root/zbar-0.10\' #echo “/usr/local/zbar/lib/” >> /etc/ld.so.conf ldconfig ln -s /usr/local/zbar/lib/pkgconfig/zbar.pc /usr/lib64/pkgconfig/zbar.pc
3.安装php-zbarcode(https://github.com/mkoppanen/php-zbarcode)
这个步骤有问题
zbarcode.c
错误一 :环境为php7才可以编译 但是通过phpInfo() 查看就是php7 无解
^ /root/php-zbarcode-master/zbarcode.c: 在函数‘s_php_zbarcode_scan_page’中: /root/php-zbarcode-master/zbarcode.c:275:57: 错误:宏“add_assoc_string”传递了 4 个参数,但只需要 3 个 add_assoc_string(symbol_array, "data", (char *)data, 1); ^ /root/php-zbarcode-master/zbarcode.c:275:3: 错误:‘add_assoc_string’未声明(在此函数内第一次使用) add_assoc_string(symbol_array, "data", (char *)data, 1); ^ /root/php-zbarcode-master/zbarcode.c:275:3: 附注:每个未声明的标识符在其出现的函数内只报告一次 /root/php-zbarcode-master/zbarcode.c:276:86: 错误:宏“add_assoc_string”传递了 4 个参数,但只需要 3 个 add_assoc_string(symbol_array, "type", (char *)zbar_get_symbol_name(symbol_type), 1);
/root/php-zbarcode-master/zbarcode.c: 在文件作用域:
/root/php-zbarcode-master/zbarcode.c:647:1: 错误:未知的类型名‘zend_object_value’
static zend_object_value php_zbarcode_object_new(zend_class_entry *class_type TSRMLS_DC)
^
/root/php-zbarcode-master/zbarcode.c: 在函数‘php_zbarcode_object_new’中:
/root/php-zbarcode-master/zbarcode.c:650:2: 错误:未知的类型名‘zend_object_value’
错误二
zend_object_value retval; ^ /root/php-zbarcode-master/zbarcode.c:660:8: 错误:在非结构或联合中请求成员‘handle’ retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_zbarcode_object_free_storage, NULL TSRMLS_CC); ^ /root/php-zbarcode-master/zbarcode.c:660:56: 错误:‘zend_objects_free_object_storage_t’未声明(在此函数内第一次使用) retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_zbarcode_object_free_storage, NULL TSRMLS_CC); ^ /root/php-zbarcode-master/zbarcode.c:660:92: 错误:expected ‘)’ before ‘php_zbarcode_object_free_storage’ retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_zbarcode_object_free_storage, NULL TSRMLS_CC);
等等...
wget https://github.com/mkoppanen/php-zbarcode/archive/master.zip unzip php-zbarcode-master.zip cd php-zbarcode-master /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install #提示如下完成 #Build complete. #Don\'t forget to run \'make test\'. #Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
4.添加:extension=zbarcode.so 到php.ini配置文件
此时查看phpinfo();后搜索zbarcode后为完成
5.测试效果
图片取自https://en.wikipedia.org/wiki/File:Ean-13-5901234123457.png
/* Create new image object */ $image = new ZBarCodeImage("test.jpg"); /* Create a barcode scanner */ $scanner = new ZBarCodeScanner(); /* Scan the image */ $barcode = $scanner->scan($image); /* Loop through possible barcodes */ if (!empty($barcode)) { foreach ($barcode as $code) { printf("Found type %s barcode with data %s\n", $code[\'type\'], $code[\'data\']); } }