登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: TP整合phpqrcode生成二维码[zt]     [回主站]     [分站链接]
TP整合phpqrcode生成二维码[zt]
clq
浏览(442) - 2018-06-25 21:05:11 发表 编辑

关键字: php

[2019-08-26 13:53:02 最后更新]
https://blog.csdn.net/maxiaojingabc/article/details/70570785
--------------------------------------------------
TP整合phpqrcode生成二维码
2017年04月24日 10:31:02
阅读数:4316
下载phpqrcode

下载地址:http://phpqrcode.sourceforge.net/
1、合到Thinkphp框架

在“ThinkPHP\Library\Vendor\”下新建目录phpqrcode,将压缩包内容解压到该文件夹下。
2、用phpqrcode生成二维码

在IndexController控制器下添加如下方法:

public function qrcode($url="www.baidu.com",$level=3,$size=4)
    {
              Vendor('phpqrcode.phpqrcode');
              $errorCorrectionLevel =intval($level) ;//容错级别
              $matrixPointSize = intval($size);//生成图片大小
             //生成二维码图片
              $object = new \QRcode();
              $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);  
    }

访问:http://127.0.0.1/Index/qrcode即可看到生成的二维码。
3、成带logo的二维码

先调用phpqrcode生成一张二维码,再使用PHP的image相关函数将logo图片添加到生成的二维码图片上。

include 'phpqrcode.php';   
$value = 'http://www.cnblogs.com/txw1958/'; //二维码内容  
$errorCorrectionLevel = 'L';//容错级别  
$matrixPointSize = 6;//生成图片大小  
//生成二维码图片  
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);  
$logo = '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, 'helloweixin.png');  
echo '<img src="helloweixin.png">';



clq  2018-06-25 21:09:30 发表 编辑

tpshop 中调用更简单

    //参考 application\common\logic\WechatLogic.php
    //生成二维码
    public function CreateQRCode($str)
    {

        $qrText = $str;

       
        //http://localhost:8080/site2/index.php/Mobile/User/reg?code=65
       
        $qrText = __SITE_URL__ . "/index.php/Mobile/User/reg?code=". $this->user_id;

        vendor('phpqrcode.phpqrcode');
        vendor('topthink.think-image.src.Image');
       
        $qr_code_path = './public/upload/qr_code/';
        !file_exists($qr_code_path) && mkdir($qr_code_path, 0777, true);

        /* 生成二维码 */
        ////$qr_code_file = $qr_code_path.time().rand(1, 10000).'.png'; //这个是随机文件名,如何得到用户 id 呢
        $qr_code_file = './public/upload/qr_code/' . $this->user_id .'.png';
       
        //\QRcode::png($qrText, $qr_code_file, QR_ECLEVEL_M); //这个调用方法非常奇特
        \QRcode::png($qrText, $qr_code_file, QR_ECLEVEL_M, 8, 1); //这个调用方法非常奇特
        //第四个参数$size,控制生成图片的大小,默认为4
        //第五个参数$margin,控制生成二维码的空白区域大小


        return $qr_code_file;
       
    }//

--------------------------------------------------
不过 QRcode 前面那个 "\" 实在是怪异。据说是什么命名空间。


clq  2018-06-25 21:18:56 发表 编辑

http://www.php.net/manual/zh/language.namespaces.basics.php


<?php
namespace Foo\Bar;
include 'file1.php';

const FOO = 2;
function foo() {}
class foo
{
    static function staticmethod() {}
}

/* 非限定名称 */
foo(); // 解析为 Foo\Bar\foo resolves to function Foo\Bar\foo
foo::staticmethod(); // 解析为类 Foo\Bar\foo的静态方法staticmethod。resolves to class Foo\Bar\foo, method staticmethod
echo FOO; // resolves to constant Foo\Bar\FOO

/* 限定名称 */
subnamespace\foo(); // 解析为函数 Foo\Bar\subnamespace\foo
subnamespace\foo::staticmethod(); // 解析为类 Foo\Bar\subnamespace\foo,
                                  // 以及类的方法 staticmethod
echo subnamespace\FOO; // 解析为常量 Foo\Bar\subnamespace\FOO
                                  
/* 完全限定名称 */
\Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo
\Foo\Bar\foo::staticmethod(); // 解析为类 Foo\Bar\foo, 以及类的方法 staticmethod
echo \Foo\Bar\FOO; // 解析为常量 Foo\Bar\FOO
?>




总数:2 页次:1/1 首页 尾页  
总数:2 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1