php QrReader解析二维码数据

在客户端不方便解析二维码图片时,通常是将图片进行base64编码,然后发送给后台服务器进行及解析

安装

#!/usr/bin/env php
composer require khanamiryan/qrcode-detector-decoder

使用

<?php
use Zxing\QrReader;
//使用
if(isset($_POST) and isset($_POST['url_base64'])){
    echo  base64_text($_POST['url_base64']);
}
//定义方法
function base64_text($str){
        $base_img = str_replace('data:image/jpg;base64,', '',$str);
       // 临时文件目录
       $path =dirname(dirname(dirname(__DIR__))).'/runtime/file/'
        //临时文件前缀
        $prefix = "img_";
        //临时文件名称
        $output_file = $prefix.time().rand(100,999).'.jpg';
        $path .=$output_file;
        if( file_put_contents($path, base64_decode($base_img))){
            $qrcode = new QrReader($path);
            $text = $qrcode->text();
            unset($qrcode);
            unlink( $path);
            return $text;
        }
         return false;
    }
>
This entry was posted in php. Bookmark the permalink.

发表回复