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

18 篇文章 0 订阅
18 篇文章 0 订阅

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("\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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值