说明:扩展需要依赖ImageMagick和zbar,安装前先安装这两个软件
安装ImageMagick(http://www.imagemagick.org/)
yum install ImageMagick.x86_64 ImageMagick-devel.x86_64
安装zbar(http://sourceforge.net/projects/zbar/?source=directory)
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
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
安装php-zbarcode(https://github.com/mkoppanen/php-zbarcode)
注:php7 请使用 https://github.com/jorissteyn/php-zbarcode/tree/gophp7
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/
添加:extension=zbarcode.so 到php.ini配置文件
此时查看phpinfo();后搜索zbarcode后为完成
测试效果
图片取自https://en.wikipedia.org/wiki/File:Ean-13-5901234123457.png
<?php
//新建一个图像对象
$image = new ZBarCodeImage("./test.png");
// 创建一个二维码识别器
$scanner = new ZBarCodeScanner();
//识别图像
$barcode = $scanner->scan($image);
//循环输出二维码信息
if (!empty($barcode)) {
foreach ($barcode as $code) {
printf("Found type %s barcode with data %s\n", $code['type'], $code['data']);
}
}
?>