一、获取PHP版Zxing扩展库
官方仓库地址为:https://github.com/khanamiryan/php-qrcode-detector-decoder
二、部署与调用Zxing扩展库
下载完成后解压,项目结构中仅需保留lib文件夹。

将lib文件夹重命名为Zxing,随后打开该目录下的QrReader.php文件,确认其命名空间已定义为Zxing。

接着,将重命名后的Zxing文件夹整体迁移至ThinkPHP框架的extend扩展目录下。

若直接运行,可能会遇到如下报错 Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in
该错误源于PHP进程可用内存不足。
解决方法是在实例化QrReader之前,调用ini_set()函数动态提高内存上限。
//修改php内存限制为1024M
ini_set('memory_limit','1024M');
此外,还可能遭遇 Call to undefined function Zxing\Common\fill_array() 这类错误。
需编辑Zxing目录中的QrReader.php文件,手动引入common/customFunctions.php文件以补全依赖。
<?php
namespace Zxing;
use Zxing\Common\HybridBinarizer;
use Zxing\Qrcode\QRCodeReader;
include_once('common/customFunctions.php');
final class QrReader
{
}
修改后的QrReader.php完整代码如下所示:
<?php
namespace Zxing;
use Zxing\Common\HybridBinarizer;
use Zxing\Qrcode\QRCodeReader;
include_once('common/customFunctions.php');
final class QrReader
{
const SOURCE_TYPE_FILE = 'file';
const SOURCE_TYPE_BLOB = 'blob';
const SOURCE_TYPE_RESOURCE = 'resource';
private $bitmap;
private $reader;
private $result;
public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true)
{
if (!in_array($sourceType, [
self::SOURCE_TYPE_FILE,
self::SOURCE_TYPE_BLOB,
self::SOURCE_TYPE_RESOURCE,
], true)) {
throw new \InvalidArgumentException('Invalid image source.');
}
$im = null;
switch ($sourceType) {
case QrReader::SOURCE_TYPE_FILE:
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$im = new \Imagick();
$im->readImage($imgSource);
} else {
$image = file_get_contents($imgSource);
$im = imagecreatefromstring($image);
}
break;
case QrReader::SOURCE_TYPE_BLOB:
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$im = new \Imagick();
$im->readImageBlob($imgSource);
} else {
$im = imagecreatefromstring($imgSource);
}
break;
case QrReader::SOURCE_TYPE_RESOURCE:
$im = $imgSource;
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$useImagickIfAvailable = true;
} else {
$useImagickIfAvailable = false;
}
break;
}
if ($useImagickIfAvailable && extension_loaded('imagick')) {
if (!$im instanceof \Imagick) {
throw new \InvalidArgumentException('Invalid image source.');
}
$width = $im->getImageWidth();
$height = $im->getImageHeight();
$source = new IMagickLuminanceSource($im, $width, $height);
} else {
if (!is_resource($im)) {
throw new \InvalidArgumentException('Invalid image source.');
}
$width = imagesx($im);
$height = imagesy($im);
$source = new GDLuminanceSource($im, $width, $height);
}
$histo = new HybridBinarizer($source);
$this->bitmap = new BinaryBitmap($histo);
$this->reader = new QRCodeReader();
}
public function decode()
{
try {
$this->result = $this->reader->decode($this->bitmap);
} catch (NotFoundException $er) {
$this->result = false;
} catch (FormatException $er) {
$this->result = false;
} catch (ChecksumException $er) {
$this->result = false;
}
}
public function text()
{
$this->decode();
if (method_exists($this->result, 'toString')) {
return $this->result->toString();
}
return $this->result;
}
public function getResult()
{
return $this->result;
}
}
完成上述配置后,即可在业务代码中进行调用。
//引用
use Zxing\QrReader;
//调用类库
$qrcode = new QrReader("二维码图片路径");
$content = $qrcode->text();
至此,关于在ThinkPHP环境中利用Zxing扩展库解析二维码内容的详细图文教程已结束。如需了解更多相关技术细节,建议查阅本站往期文章或持续关注后续更新,感谢大家的支持!