php语言 百文网手机站

PHP学习:QRCode PHP生成二维码类库

时间:2021-04-13 16:31:18 php语言 我要投稿

PHP学习:QRCode PHP生成二维码类库

  在PHP语言中怎么生成二维码类库呢?下面就和小编一起来看看吧!希望对大家有用,更多内容请关注应届毕业生网!

PHP学习:QRCode PHP生成二维码类库

  使用类库的方法

1
2
3
include("Common/QRCode.class.php");
$QRCodenew QRCode();
$categoryList $QRCode->getUrl();

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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($pathor ($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;
        }
    }
 
}
 
?>

【PHP学习:QRCode PHP生成二维码类库】相关文章:

php生成带logo二维码方法09-03

PHP封装数据库操作类09-26

php学习之php配置09-09

PHP访问数据库09-05

php备份数据库类的方法09-30

php学习之php预定义变量09-16

PHP学习路线以及PHP优化技巧09-08

PHP新手之学习类与对象09-14

php怎么生成随机密码09-29

PHP生成Excel报表的方法09-08