您的位置:首页 > 编程语言 > PHP开发

PHP生成一张含有二维码的图片(文章末尾附代码下载链接)

2018-03-05 16:30 911 查看
PHP生成一张图片用到的类有QRcode,QRencode ,QRtools , QRimage这四个类是主要的。
主导类: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: