PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
2016-11-27 16:36
801 查看
PHP生成一张图片用到的类有QRcode,QRencode ,QRtools , QRimage这四个类是主要的。
主导类:QRcode
辅助类:QRencode, QRimage ,QRtools .
请看下图, 直接贴源码!!!
1、直接调用函数(参数赋值,返回图片);
2、转码过程,进行工厂模式转化;
3、记录过程信息
4、生成二维码
5、输出图片
这是主要的代码,全部代码稍后上传!
下载地址: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); }
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; }
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()); } }
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; }
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); }
这是主要的代码,全部代码稍后上传!
下载地址:http://download.csdn.net/detail/u013703963/9694960
相关文章推荐
- PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- php 点击链接图片下载程序代码
- php生成二维码不保存服务器还有下载功能的实现代码
- php 生成图片带二维码,并且强制用户下载
- PHP生成迅雷、快车、旋风等软件的下载链接代码实例
- php实现生成带二维码图片并强制下载功能
- 随机字符串生成图片 PHP代码
- 抓取并下载CSS中所有图片文件的php代码
- 利用PHP为大图片生成缩略图代码实现
- 让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
- javascript 图片上一张下一张链接效果代码
- php让图片可以下载的代码第1/2页
- 改进的某生成验证码图片的PHP代码
- php文章内容分页并生成相应的htm静态页面代码
- php生成SessionID和图片校验码的思路和实现代码
- PHP 文章中的远程图片采集到本地的代码