0712-2888027 189-8648-0214
微信公众号

孝感风信网络科技有限公司微信公众号

当前位置:主页 > 技术支持 > PHPCMS > phpcms增加二维码生成功能

phpcms增加二维码生成功能

时间:2024-03-29来源:风信官网 点击: 593次
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="ps-twocode" id="phone_dimcode_pic">
    {CreateQRcode($url, 'H', '6')}                                        
</div>

出来的效果如下截图:
phpcms增加二维码生成功能
热门关键词: phpcms 二维码生成功能
栏目列表
推荐内容
热点内容
展开