ThinkPHP利用phpqrcode生成二维码(带logo)

首先需要引入phpqrcode文件。文件下载:phpqrcode.rar


1
2
3
4
5
6
7
8
9
< img alt = "二维码" src = "http://www.xcsoft.cn/public/qrcode?text=填写需要生成的数据&size=4&level=L&padding=2&logo=" >
<!---
调用说明
text:填写您想到生成的数据,如:http://www.xcsoft.cn
size:填写生成图片大小,1为25*25 2为50*50 以此类推 最大是10:250*250
level:填写纠错大小 一般为L即可,可选:L、M、Q、H
padding:图片填充白色区域的大小 默认为2
logo:如果您希望生成的二维码中间带logo,请填写您的logo完整地址 大小建议在30*30左右,可根据您设定的size来调节合适大小,没有则不填
--->

官方默认程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
     //引入phpqrcode文件
     include "phpqrcode/phpqrcode.php" ;
     //要生成二维码的数据
     $text = "http://www.xcsoft.cn" ;
     //纠错级别, 纠错级别越高,生成图片会越大
     //L水平    7%的字码可被修正
     //M水平    15%的字码可被修正
     //Q水平    25%的字码可被修正
     //H水平    30%的字码可被修正
     $level = "L" ;
     //图片每个黑点的像素。
     $size = "4" ;
     //生成图片 第二个参数:是否保存成文件 如需要保存文件,第二个参数改为文件名即可,如:'qrcode.png'
     QRcode::png( $text , false, $level , $size );
?>

       需要兼容到ThinkPHP里,则需要把下载到的文件复制到Vendor夹里。

ThinkPHP代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//引用地址http://www.xcsoft.cn/public/qrcode
//text:需要生成二维码的数据,默认:http://www.xcsoft.cn
//size:图片每个黑点的像素,默认4
//level:纠错等级,默认L
//padding:图片外围空白大小,默认2
//logo:全地址,默认为空
//完整引用地址:http://www.xcsoft.cn/public/qrcode?text=http://www.xcsoft.cn&size=4&level=L&padding=2&logo=http://www.xcsoft.cn/Public//images/success.png
public function qrcode( $text = 'http://www.xcsoft.cn' , $size = '4' , $level = 'L' , $padding =2, $logo =true){
     $text = $this ->_get( 'text' )? $this ->_get( 'text' ): $text ;
     $size = $this ->_get( 'size' )? $this ->_get( 'size' ): $size ;
     $level = $this ->_get( 'level' )? $this ->_get( 'level' ): $level ;
     $logo = $this ->_get( 'logo' )? $this ->_get( 'logo' ): $logo ;
     $padding = $this ->_get( 'padding' )? $this ->_get( 'padding' ): $padding ;
     $path = './Uploads/qrcode/' ;
     $QR = $path . 'qrcode.png' ;
     vendor( "phpqrcode.phpqrcode" );
         QRcode::png( $text , $QR , $level , $size , $padding );
     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_height = imagesy( $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 );
     }
     header( "Content-Type:image/jpg" );
     imagepng( $QR );
}

       写好后,图片地址直接引用即可,引用详参见下方(本站已实现)。

引用说明

       引用地址:http://www.xcsoft.cn/public/qrcode

       text:需要生成二维码的数据,默认:http:www.xcsoft.cn

       size:图片每个黑点的像素,默认4

       level:纠错等级,默认L

       padding:默认2 图片外围空白大小

       logo:完整地址,默认为空

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值