入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2016-11-27 投稿人: 浏览次数:1544

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);
}

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("
", $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

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。