phpcms增加二维码生成功能的两种方法

第一种方法,使用PHP原声来生成

PHP环境必须开启支持GD2
1、api 接口文件目录增加如下文件:
官网下载地址:http://phpqrcode.sourceforge.net/
官方示例地址:http://phpqrcode.sourceforge.net/examples/index.php
phpqrcode.php
2、在phpcms框架主目录functions中我们增加一个自定义函数:
\phpcms\libs\functions\extention.func.php
增加的二维码函数如下:

/**
*  extention.func.php 用户自定义函数库
*
* @copyright            (C) 2005-2010 PHPCMS
* @license                http://www.phpcms.cn/license/
* @lastmodify            2010-10-27
*/
/**
* 二维码生成函数
* @param string     $value                 二维码内容
* @param intval     $matrixPointSize    生成图片大小
* @param string     $errorCorrectionLevel            容错级别
* @return string   返回
*/
function CreateQRcode($value, $errorCorrectionLevel='H', $matrixPointSize = 6)
{
   
    require_once 'api/phpqrcode.php';
    //$errorCorrectionLevel = 'L';        //容错级别  
    //$matrixPointSize = 6;                //生成图片大小  
   
    //生成二维码图片  
   
    QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
   
    /*
    QRcode::png 参数说明
    第一个参数$text,就是上面代码里的URL网址参数,
  第二个参数$outfile默认为否,不生成文件,只将二维码图片返回,否则需要给出存放生成二维码图片的路径
  第三个参数$level默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。
  利用二维维码的容错率,我们可以将头像放置在生成的二维码图片任何区域。
  第四个参数$size,控制生成图片的大小,默认为4
  第五个参数$margin,控制生成二维码的空白区域大小
  第六个参数$saveandprint,保存二维码图片并显示出来,$outfile必须传递图片路径。
    */
   
    $logo = 'statics/images/qrcode_logo.png';    //准备好的logo图片  
    $QR = 'qrcode.png';                //已经生成的原始二维码图  
   
    if ($logo !== FALSE) {  
        $QR = imagecreatefromstring(file_get_contents($QR));  
        $logo = imagecreatefromstring(file_get_contents($logo));  
        $QR_width = imagesx($QR);        //二维码图片宽度  
        $QR_height = imagesy($QR);        //二维码图片高度  
        $logo_width = imagesx($logo);    //logo图片宽度  
        $logo_height = imagesy($logo);    //logo图片高度  
        $logo_qr_width = $QR_width / 5;  
        $scale = $logo_width/$logo_qr_width;  
        $logo_qr_height = $logo_height/$scale;  
        $from_width = ($QR_width - $logo_qr_width) / 2;
         
        //重新组合图片并调整大小  
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);  
    }
   
   
    //返回
    imagepng($QR, 'thisqrcode.png');  
    return '<img src="thisqrcode.png" />';
}

在内容页模板中的使用show.html:

<div class="qrcode">
{CreateQRcode($url, 'H', '6')}
</div>

第二种方法是使用js来生成:

下载附件,然后在页面引入JS文件,在需要出现二维码的地方加上下面代码

<!-- 二维码 -->
<div id="qrcodeCanvas"></div>
<script>
            $('#qrcodeCanvas').qrcode({
                text: '{$url}',//二维码代表的字符串(本页面的URL)
                width: 167,//二维码宽度
                height: 167//二维码高度
            });
</script>

完整代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>二维码生成</title>
    <!-- 二维码 -->
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery.qrcode.js"></script>
    <script type="text/javascript" src="js/qrcode.js"></script>
</head>
<body>
        <div id="qrcodeCanvas"></div>
        <script>
            $('#qrcodeCanvas').qrcode({
                text: '{$url}',//二维码代表的字符串(本页面的URL)
                width: 167,//二维码宽度
                height: 167//二维码高度
            });
        </script>
</body>
</html>

qrcode.zip

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/295.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。