您现在的位置是:首页 > PHP类库

李清波 2015-09-27 PHP类库 4982 复制当前网址

QRCode PHP生成二维码类库


使用类库的方法

include("Common/QRCode.class.php");
$QRCode= new QRCode();
$categoryList = $QRCode->geturl();


以下是php生成二维码完整类库

<?php

/**
 * 类功能:将指定uRL利用google api生成二维码保存到本地并返回本地访问url
 * author:252588119@qq.com
 * 使用方法见:http://liqingbo.cn/blog-435.html
 */
class QRCode {

    private $path;
    private $size;

    public function __construct($path, $size) {
        $this->path = empty($path) ? C('webPath') . "/uploads/QRCode/" : $path;
        $this->size = empty($size) ? 80 : $size;
    }

    /**
     * 检测存储目录是否存在,不存在则创建该目录
     */
    private function makeDir($path) {
        return is_dir($path) or ($this->makeDir(dirname($path)) and @mkdir($path, 0777));
    }

    /**
     * 取得二维码地址
     */
    public function geturl($url = "http://liqingbo.cn/blog-435.html") {
        $inPath = 'http://chart.apis.google.com/chart?chs=' . $this->size . 'x' . $this->size . '&cht=qr&chld=L|0&chl=' . $url;
        $savePath = $_SERVER['DOCuMENT_ROOT'] . $this->path;
        $this->makeDir($savePath);
        $fileName = substr(md5("$url"), 8, 16) . "_" . $this->size . ".png";

        $savePath.=$fileName;
        $outurl = "http://" . $_SERVER['HTTP_HOST'] . $this->path . $fileName;
        if (file_exists($savePath) && filesize($savePath) > 0) {
            return $outurl;
        }
        $in = fopen($inPath, "rb");
        $out = fopen($savePath, "wb");
        while ($chunk = fread($in, 8192))
            fwrite($out, $chunk, 8192);
        fclose($in);
        fclose($out);
        if (filesize($savePath) == 0) {
            $this->geturl($url);
        } else {
            return $outurl;
        }
    }

}

?>


文章来源:https://www.liqingbo.com/blog-435.html

评论