Download the Zxing extension library for PHP

Download at github.com/khanamiryan…

Use the Zxing extension library

1. After downloading the file, decompress it directly. The structure is as follows, we only need the folder lib

2. Rename the lib folder to Zxing, and open the qrreader.php file in Zxing directory

The next step is as simple as placing the Zxing folder in thnikphp’s extend directory

 

Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in

  • Error: PHP memory is insufficient
  • Solution: Before calling QrReader, change the memory limit using the ini_set() method
// Change the PHP memory limit to 1024MB
ini_set('memory_limit'.'1024M');
Copy the code

Call to undefined function Zxing\Common\fill_array()

Solution: modify the Zxing directory QrReader. PHP file, load the common/customFunctions. The PHP file, as follows:


      
namespace Zxing;

use Zxing\Common\HybridBinarizer;
use Zxing\Qrcode\QRCodeReader;
include_once('common/customFunctions.php');

final class QrReader
{}Copy the code

Qrread.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; }}Copy the code

6. Call in code

/ / reference
use Zxing\QrReader;
// Call the class library
$qrcode = new QrReader("Two-dimensional code picture Path"); 
$content = $qrcode->text();
Copy the code