phpqrcode 实现url转二维码

这篇博客介绍了如何利用phpqrcode库将URL转换为二维码。文章中提供了相关源码,包括qrimage.php、qrinput.php、qrbitstream.php、qrsplit.php、qrrscode.php、qrmask.php、qrencode.php等类文件的定义,展示了QR码编码过程的关键步骤。
摘要由CSDN通过智能技术生成

引用第三方类库phpqrcode
附上源码(可复制后报错为QRcode.php):

<?php namespace phpqrcode; /* * PHP QR Code encoder * * This file contains MERGED version of PHP QR Code library. * It was auto-generated from full version for your convenience. * * This merged version was configured to not requre any external files, * with disabled cache, error loging and weker but faster mask matching. * If you need tune it up please use non-merged version. * * For full version, documentation, examples of use please visit: * * http://phpqrcode.sourceforge.net/ * https://sourceforge.net/projects/phpqrcode/ * * PHP QR Code is distributed under LGPL 3 * Copyright (C) 2010 Dominik Dzienia * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /* * Version: 1.1.4 * Build: 2010100721 */ //---- qrconst.php ----------------------------- /* * PHP QR Code encoder * * Common constants * * Based on libqrencode C library distributed under LGPL 2.1 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi

//---- qrimage.php -----------------------------

/*

  • PHP QR Code encoder

  • Image output of code using GD2

  • PHP QR Code is distributed under LGPL 3

  • Copyright © 2010 Dominik Dzienia

  • This library is free software; you can redistribute it and/or

  • modify it under the terms of the GNU Lesser General Public

  • License as published by the Free Software Foundation; either

  • version 3 of the License, or any later version.

  • This library is distributed in the hope that it will be useful,

  • but WITHOUT ANY WARRANTY; without even the implied warranty of

  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

  • Lesser General Public License for more details.

  • You should have received a copy of the GNU Lesser General Public

  • License along with this library; if not, write to the Free Software

  • Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    */

    define(‘QR_IMAGE’, true);

    class 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);
     }
    
     //----------------------------------------------------------------------
     public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) 
     {
         $image = self::image($frame, $pixelPerPoint, $outerFrame);
         
         if ($filename === false) {
             Header("Content-type: image/jpeg");
             ImageJpeg($image, null, $q);
         } else {
             ImageJpeg($image, $filename, $q);            
         }
         
         ImageDestroy($image);
     }
    
     //----------------------------------------------------------------------
     private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) 
     {
         $h = count($frame);
         $w = strlen($frame[0]);
         
         $imgW = $w + 2*$outerFrame;
         $imgH = $h + 2*$outerFrame;
         
         $base_image =ImageCreate($imgW, $imgH);
         
         $col[0] = ImageColorAllocate($base_image,255,255,255);
         $col[1] = ImageColorAllocate($base_image,0,0,0);
    
         imagefill($base_image, 0, 0, $col[0]);
    
         for($y=0; $y<$h; $y++) {
             for($x=0; $x<$w; $x++) {
                 if ($frame[$y][$x] == '1') {
                     ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]); 
                 }
             }
         }
         
         $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
         ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
         ImageDestroy($base_image);
         
         return $target_image;
     }
    

    }

//---- qrinput.php -----------------------------

/*

  • PHP QR Code encoder

  • Input encoding class

  • Based on libqrencode C library distributed under LGPL 2.1

  • Copyright © 2006, 2007, 2008, 2009 Kentaro Fukuchi fukuchi@megaui.net

  • PHP QR Code is distributed under LGPL 3

  • Copyright © 2010 Dominik Dzienia

  • This library is free software; you can redistribute it and/or

  • modify it under the terms of the GNU Lesser General Public

  • License as published by the Free Software Foundation; either

  • version 3 of the License, or any later version.

  • This library is distributed in the hope that it will be useful,

  • but WITHOUT ANY WARRANTY; without even the implied warranty of

  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

  • Lesser General Public License for more details.

  • You should have received a copy of the GNU Lesser General Public

  • License along with this library; if not, write to the Free Software

  • Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    */

    define(‘STRUCTURE_HEADER_BITS’, 20);
    define(‘MAX_STRUCTURED_SYMBOLS’, 16);

    class QRinputItem {

     public $mode;
     public $size;
     public $data;
     public $bstream;
    
     public function __construct($mode, $size, $data, $bstream = null) 
     {
         $setData = array_slice($data, 0, $size);
         
         if (count($setData) < $size) {
             $setData = array_merge($setData, array_fill(0,$size-count($setData),0));
         }
     
         if(!QRinput::check($mode, $size, $setData)) {
             throw new Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',',$setData));
             return null;
         }
         
         $this->mode = $mode;
         $this->size = $size;
         $this->data = $setData;
         $this->bstream = $bstream;
     }
     
     //----------------------------------------------------------------------
     public function encodeModeNum($version)
     {
         try {
         
             $words = (int)($this->size / 3);
             $bs = new QRbitstream();
             
             $val = 0x1;
             $bs->appendNum(4, $val);
             $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size);
    
             for($i=0; $i<$words; $i++) {
                 $val  = (ord($this->data[$i*3  ]) - ord('0')) * 100;
                 $val += (ord($this->data[$i*3+1]) - ord('0')) * 10;
                 $val += (ord($this->data[$i*3+2]) - ord('0'));
                 $bs->appendNum(10, $val);
             }
    
             if($this->size - $words * 3 == 1) {
                 $val = ord($this->data[$words*3]) - ord('0');
                 $bs->appendNum(4, $val);
             } else if($this->size - $words * 3 == 2) {
                 $val  = (ord($this->data[$words*3  ]) - ord('0')) * 10;
                 $val += (ord($this->data[$words*3+1]) - ord('0'));
                 $bs->appendNum(7, $val);
             }
    
             $this->bstream = $bs;
             return 0;
             
         } catch (Exception $e) {
             return -1;
         }
     }
     
     //----------------------------------------------------------------------
     public function encodeModeAn($version)
     {
         try {
             $words = (int)($this->size / 2);
             $bs = new QRbitstream();
             
             $bs->appendNum(4, 0x02);
             $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size);
    
             for($i=0; $i<$words; $i++) {
                 $val  = (int)QRinput::lookAnTable(ord($this->data[$i*2  ])) * 45;
                 $val += (int)QRinput::lookAnTable(ord($this->data[$i*2+1]));
    
                 $bs->appendNum(11, $val);
             }
    
             if($this->size & 1) {
                 $val = QRinput::lookAnTable(ord($this->data[$words * 2]));
                 $bs->appendNum(6, $val);
             }
     
             $this->bstream = $bs;
             return 0;
         
         } catch (Exception $e) {
             return -1;
         }
     }
     
     //----------------------------------------------------------------------
     public function encodeMode8($version)
     {
         try {
             $bs = new QRbitstream();
    
             $bs->appendNum(4, 0x4);
             $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size);
    
             for($i=0; $i<$this->size; $i++) {
                 $bs->appendNum(8, ord($this->data[$i]));
             }
    
             $this->bstream = $bs;
             return 0;
         
         } catch (Exception $e) {
             return -1;
         }
     }
     
     //----------------------------------------------------------------------
     public function encodeModeKanji($version)
     {
         try {
    
             $bs = new QRbitrtream();
             
             $bs->appendNum(4, 0x8);
             $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int)($this->size / 2));
    
             for($i=0; $i<$this->size; $i+=2) {
                 $val = (ord($this->data[$i]) << 8) | ord($this->data[$i+1]);
                 if($val <= 0x9ffc) {
                     $val -= 0x8140;
                 } else {
                     $val -= 0xc140;
                 }
                 
                 $h = ($val >> 8) * 0xc0;
                 $val = ($val & 0xff) + $h;
    
                 $bs->appendNum(13, $val);
             }
    
             $this->bstream = $bs;
             return 0;
         
         } catch (Exception $e) {
             return -1;
         }
     }
    
     //----------------------------------------------------------------------
     public function encodeModeStructure()
     {
         try {
             $bs =  new QRbitstream();
             
             $bs->appendNum(4, 0x03);
             $bs->appendNum(4, ord($this->data[1]) - 1);
             $bs->appendNum(4, ord($this->data[0]) - 1);
             $bs->appendNum(8, ord($this->data[2]));
    
             $this->bstream = $bs;
             return 0;
         
         } catch (Exception $e) {
             return -1;
         }
     }
     
     //----------------------------------------------------------------------
     public function estimateBitStreamSizeOfEntry($version)
     {
         $bits = 0;
    
         if($version == 0) 
             $version = 1;
    
         switch($this->mode) {
             case QR_MODE_NUM:        $bits = QRinput::estimateBitsModeNum($this->size);    break;
             case QR_MODE_AN:        $bits = QRinput::estimateBitsModeAn($this->size);    break;
             case QR_MODE_8:            $bits = QRinput::estimateBitsMode8($this->size);    break;
             case QR_MODE_KANJI:        $bits = QRinput::estimateBitsModeKanji($this->size);break;
             case QR_MODE_STRUCTURE:    return STRUCTURE_HEADER_BITS;            
             default:
                 return 0;
         }
    
         $l = QRspec::lengthIndicator($this->mode, $version);
         $m = 1 << $l;
         $num = (int)(($this->size + $m - 1) / $m);
    
         $bits += $num * (4 + $l);
    
         return $bits;
     }
     
     //----------------------------------------------------------------------
     public function encodeBitStream($version)
     {
         try {
         
             unset($this->bstream);
             $words = QRspec::maximumWords($this->mode, $version);
             
             if($this->size > $words) {
             
                 $st1 = new QRinputItem($this->mode, $words, $this->data);
                 $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words));
    
                 $st1->encodeBitStream($version);
                 $st2->encodeBitStream($version);
                 
                 $this->bstream = new QRbitstream();
                 $this->bstream->append($st1->bstream);
                 $this->bstream->append($st2->bstream);
                 
                 unset($st1);
                 unset($st2);
                 
             } else {
                 
                 $ret = 0;
                 
                 switch($this->mode) {
                     case QR_MODE_NUM:        $ret = $this->encodeModeNum($version);    break;
                     case QR_MODE_AN:        $ret = $this->encodeModeAn($version);    break;
                     case QR_MODE_8:            $ret = $this->encodeMode8($version);    break;
                     case QR_MODE_KANJI:        $ret = $this->encodeModeKanji($version);break;
                     case QR_MODE_STRUCTURE:    $ret = $this->encodeModeStructure();    break;
                     
                     default:
                         break;
                 }
                 
                 if($ret < 0)
                     return -1;
             }
    
             return $this->bstream->size();
         
         } catch (Exception $e) {
             return -1;
         }
     }
    

    };

    //##########################################################################

    class QRinput {

     public $items;
     
     private $version;
     private $level;
     
     //----------------------------------------------------------------------
     public function __construct($version = 0, $level = QR_ECLEVEL_L)
     {
         if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
             throw new Exception('Invalid version no');
             return NULL;
         }
         
         $this->version = $version;
         $this->level = $level;
     }
     
     //----------------------------------------------------------------------
     public function getVersion()
     {
         return $this->version;
     }
     
     //----------------------------------------------------------------------
     public function setVersion($version)
     {
         if($version < 0 || $version > QRSPEC_VERSION_MAX) {
             throw new Exception('Invalid version no');
             return -1;
         }
    
         $this->version = $version;
    
         return 0;
     }
     
     //----------------------------------------------------------------------
     public function getErrorCorrectionLevel()
     {
         return $this->level;
     }
    
     //----------------------------------------------------------------------
     public function setErrorCorrectionLevel($level)
     {
         if($level > QR_ECLEVEL_H) {
             throw new Exception('Invalid ECLEVEL');
             return -1;
         }
    
         $this->level = $level;
    
         return 0;
     }
     
     //----------------------------------------------------------------------
     public function appendEntry(QRinputItem $entry)
     {
         $thi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值