PHP生成二维码图片(可自定义logo,大小,容错)

2018-09-20T10:16:00

phpqrcode类下载地址:https://sourceforge.net/projects/phpqrcode/files/

/*生成二维码图片
    @param string $content 内容
    @param string $qr 二维码地址
    @param string $logo logo地址
    @param string $level 容错等级
    @param string $size 图片大小
    @return string
    */
   function qr($content="", $qr="", $logo="", $level="L", $size="6") {
    if(empty($content)) {
        return 'content is not empty';
    }
    \think\Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);
    if(empty($qr)) {
        $path = Env::get('ROOT_PATH') . "public/static/"; // 本地存放图片路径
        if(!file_exists($path)) {
            mkdir($path, 0777, true);
        }
        $qr = $path . time() . '.png';
    }
    QRcode::png($content, $qr, $level, $size, 2);
    if($logo) {
        if(file_exists($logo)) {
            $logo_resource = imagecreatefromstring(file_get_contents($logo));
            $qrcode_resource = imagecreatefromstring(file_get_contents($qr));
            $logo_w = imagesx($logo_resource);   // logo图片宽度
            $logo_h = imagesy($logo_resource);   // logo图片高度
            $qrcode_w = imagesx($qrcode_resource); // 二维码图片宽度
            $logo_qr_width = $qrcode_w / 5;
            $scale = $logo_w / $logo_qr_width;
            $logo_qr_height = $logo_h / $scale;
            $from_width = ($qrcode_w - $logo_qr_width) / 2;
            // 重新组合图片并调整大小
            imagecopyresampled($qrcode_resource, $logo_resource, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_w, $logo_h);
            imagepng($qrcode_resource, $qr);
        } else {
            return 'logo file is not exist';
        }
    }
    return $qr;
}?>```
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »