phpqrcode生成二维码

0 篇文章 0 订阅

下载phpqrcode类库

可以实用composer下载

将phpqrcode.php文件放到项目中 其他的文件都可以不要

主要是使用png()方法来生成二维码

   public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) 
        {
            $enc = QRencode::factory($level, $size, $margin);
            return $enc->encodePNG($text, $outfile, $saveandprint=false);
        }

 

 

生成二维码1

<?php
/**
 * Created by PhpStorm.
 * User: xym
 * Date: 2018/7/31
 * Time: 上午9:58
 * 在本地生成图片文件
 */
function createQr($url=''){
    require_once 'phpqrcode.php';

    $value = $url;					//二维码内容

    $errorCorrectionLevel = 'L';	//容错级别
    $matrixPointSize = 5;			//生成图片大小

    //生成二维码图片
    $filename = 'local'.microtime().'.png';
    QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);

    $QR = $filename;				//已经生成的原始二维码图片文件


    $QR = imagecreatefromstring(file_get_contents($QR));

    //输出图片
    imagepng($QR, 'qrcode.png');
    imagedestroy($QR);
    return '<img src="qrcode.png" alt="使用微信扫描支付">';
}

//调用查看结果
echo createQr('http://www.zhenjinedu.cn/');

 

2.生成带logo的二维码图片

 

<?php
/**
 * Created by PhpStorm.
 * User: xym
 * Date: 2018/7/31
 * Time: 下午9:34
 * 生成二维码带logo
 */
function createQr($url=''){
    require_once 'phpqrcode.php';
    $value = $url;					//二维码内容
    $errorCorrectionLevel = 'H';	//容错级别
    $matrixPointSize = 7;			//生成图片大小
    //生成二维码图片
    $filename = 'pic'.microtime().'.png';
    QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);

    $logo = 'logo.JPG'; 	//准备好的logo图片
    $QR = $filename;			//已经生成的原始二维码图

    if (file_exists($logo)) {
        $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 / 4;   	//组合之后logo的宽度(占二维码的1/5)
        $scale = $logo_width/$logo_qr_width;   	//logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height/$scale;  //组合之后logo的高度
        $from_width = ($QR_width - $logo_qr_width) / 2;   //组合之后logo左上角所在坐标点

        //重新组合图片并调整大小
        /*
         *	imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
         */
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
    }

    //输出图片
    imagepng($QR, 'qrcode_pay.png');
    imagedestroy($QR);
    imagedestroy($logo);
    return '<img src="qrcode_pay.png" alt="使用微信扫描支付">';
}

//调用查看结果
echo createQr('http://www.zhenjinedu.cn/');

3.生成的图片只在浏览器输出

<?php
/**
 * Created by PhpStorm.
 * User: xym
 * Date: 2018/7/31
 * Time: 下午9:42
 * 二维码图片只输出到浏览器,不在本地生成图片文件
 */
function createQr($url=''){
    require_once 'phpqrcode.php';

    $value = $url;					//二维码内容
    $errorCorrectionLevel = 'L';	//容错级别
    $matrixPointSize = 5;			//生成图片大小
    //生成二维码图片
    $QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2);
}
//调用查看结果
createQr('http://www.zhenjinedu.cn/');

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝麻开门2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值