PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
2018-03-05 16:30
911 查看
PHP生成一张图片用到的类有QRcode,QRencode ,QRtools , QRimage这四个类是主要的。
主导类:QRcode
辅助类:QRencode, QRimage ,QRtools .
请看下图, 直接贴源码!!!
1、直接调用函数(参数赋值,返回图片);
2
3
4
5
6
7
2、转码过程,进行工厂模式转化;
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
3、记录过程信息
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
4、生成二维码
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
5、输出图片
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这是主要的代码,全部代码稍后上传!
下载地址:http://download.csdn.net/detail/u013703963/9694960
主导类:QRcode
辅助类:QRencode, QRimage ,QRtools .
请看下图, 直接贴源码!!!
1、直接调用函数(参数赋值,返回图片);
//QRcode public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) { $enc = QRencode::factory($level, $size, $margin); // var_dump($enc); return $enc->encodePNG($text, $outfile, $saveandprint=false); }1
2
3
4
5
6
7
2、转码过程,进行工厂模式转化;
//QRencode public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4) { // echo "run Qrencode::factory....<br/>"; $enc = new QRencode(); $enc->size = $size; $enc->margin = $margin; switch ($level.'') { case '0': case '1': case '2': case '3': $enc->level = $level; break; case 'l': case 'L': $enc->level = QR_ECLEVEL_L; break; case 'm': case 'M': $enc->level = QR_ECLEVEL_M; break; case 'q': case 'Q': $enc->level = QR_ECLEVEL_Q; break; case 'h': case 'H': $enc->level = QR_ECLEVEL_H; break; } return $enc; }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
3、记录过程信息
//QRencode public function encodePNG($intext, $outfile = false, $saveandprint=false) { // echo "run Qrencode::encodePNG....<br/>"; try { ob_start(); $tab = $this->encode($intext); $err = ob_get_contents(); //获取对象内容 ob_end_clean(); //清除 if ($err != '') QRtools::log($outfile, $err); //记录错误 $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin)); // echo "run Qrencode::maxSize....".$maxSize."<br/>".QR_PNG_MAXIMUM_SIZE."<br/>"; QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint); } catch (Exception $e) { //记录信息 QRtools::log($outfile, $e->getMessage()); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
4、生成二维码
//QRencode public function encode($intext, $outfile = false) { //echo "run Qrencode::encode....<br/>"; $code = new QRcode(); if($this->eightbit) { $code->encodeString8bit($intext, $this->version, $this->level); } else { $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive); } // QRtools::markTime('after_encode'); $binarized = QRtools::binarize($code->data); if ($outfile!== false) { file_put_contents($outfile, join("\n", $binarized)); } return $binarized; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
5、输出图片
//QRimage public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE) { $image = self::image($frame, $pixelPerPoint, $outerFrame); if ($filename === false) { Header("Content-type: image/png"); ImagePng($image); } else { if($saveandprint===TRUE){ ImagePng($image, $filename); header("Content-type: image/png"); ImagePng($image); }else{ ImagePng($image, $filename); } } ImageDestroy($image); }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这是主要的代码,全部代码稍后上传!
下载地址:http://download.csdn.net/detail/u013703963/9694960
相关文章推荐
- PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
- php生成二维码不保存服务器还有下载功能的实现代码
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- php 点击链接图片下载程序代码
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- php 生成图片带二维码,并且强制用户下载
- php实现生成带二维码图片并强制下载功能
- 自动识别HTML代码里的图片链接,并下载到服务器的指定目录源码
- php 生成图片二维码
- JAVA生成二维码图片代码
- PHP将身份证正反面两张照片合成一张图片的代码
- php二维码生成以及下载实现
- 用php实现下载生成某链接的快捷方式
- 随机字符串生成图片 PHP代码
- PHP备份数据库生成SQL文件并下载的函数代码
- 自动识别HTML代码里的图片链接,并下载到服务器的指定目录(开源)