php生成二维码



使用PHP QR Code类库创建二维码使用举例浏览器输出:<?include "phpqrcode/phpqrcode.php";$value="http://s.bookphone.cn/chinabook/index.php/adminhtml/Croles/admin";$errorCorrectionLevel = "L";$matrixPointSize = "4";QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);exit;?>文件输出二维码include('phpqrcode/phpqrcode.php');// 二维码数据$data = 'http://s.bookphone.cn';// 生成的文件名$filename = '1111.png';// 纠错级别:L、M、Q、H$errorCorrectionLevel = 'L';// 点的大小:1到10$matrixPointSize = 4;QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);生成中间带logo的二维码<?phpinclude('phpqrcode/phpqrcode.php');$value='http://xy.bookphone.cn';$errorCorrectionLevel = 'L';$matrixPointSize = 6;QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);echo "QR code generated"."<br />";$logo = 'logo.png';$QR = 'xiangyang.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_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);}imagepng($QR,'xiangyanglog.png');?>
使用PHP QR Code类库创建二维码


使用举例浏览器输出:


<?
include "phpqrcode/phpqrcode.php";
$value="http://s.bookphone.cn/chinabook/index.php/adminhtml/Croles/admin";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>


文件输出二维码


include('phpqrcode/phpqrcode.php');
// 二维码数据
$data = 'http://s.bookphone.cn';
// 生成的文件名
$filename = '1111.png';
// 纠错级别:L、M、Q、H
$errorCorrectionLevel = 'L';
// 点的大小:1到10
$matrixPointSize = 4;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);


生成中间带logo的二维码


<?php
include('phpqrcode/phpqrcode.php');
$value='http://xy.bookphone.cn';
$errorCorrectionLevel = 'L';
$matrixPointSize = 6;
QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);
echo "QR code generated"."<br />";
$logo = 'logo.png';
$QR = 'xiangyang.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_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);
}
imagepng($QR,'xiangyanglog.png');
?>
使用PHP QR Code类库创建二维码使用举例浏览器输出:<?include "phpqrcode/phpqrcode.php";$value="http://s.bookphone.cn/chinabook/index.php/adminhtml/Croles/admin";$errorCorrectionLevel = "L";$matrixPointSize = "4";QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);exit;?>文件输出二维码include('phpqrcode/phpqrcode.php');// 二维码数据$data = 'http://s.bookphone.cn';// 生成的文件名$filename = '1111.png';// 纠错级别:L、M、Q、H$errorCorrectionLevel = 'L';// 点的大小:1到10$matrixPointSize = 4;QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);生成中间带logo的二维码<?phpinclude('phpqrcode/phpqrcode.php');$value='http://xy.bookphone.cn';$errorCorrectionLevel = 'L';$matrixPointSize = 6;QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);echo "QR code generated"."<br />";$logo = 'logo.png';$QR = 'xiangyang.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_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);}imagepng($QR,'xiangyanglog.png');?>
### 使用PHP生成二维码的解决方案与库 在PHP生成二维码通常需要借助第三方库来实现。以下是一些常用的库及其使用方法: #### 1. 使用 `phpqrcode` 库生成二维码 `phpqrcode` 是一个简单易用的PHP库,可以快速生成二维码。以下是具体代码示例和说明: ```php <?php // 引入phpqrcode库文件 require_once 'path/to/phpqrcode/qrlib.php'; // 引入库文件[^1] // 要编码到二维码中的数据 $data = 'https://www.example.com'; // 指定生成二维码图片文件名 $fileName = 'qr.png'; // 使用phpqrcode库中的png方法生成二维码图片 QRcode::png($data, $fileName, 'L', 5); // 参数分别为:数据、文件名、纠错级别、点的大小 // 输出二维码图片到浏览器 header('Content-Type: image/png'); readfile($fileName); // 将二维码图片输出到浏览器 ``` #### 2. 使用 `Endroid/QrCode` 库生成二维码 `Endroid/QrCode` 是一个现代化的PHP库,支持更多高级功能。以下是其使用示例: ```php <?php // 引入库文件 require 'vendor/autoload.php'; // 引入Composer自动加载文件[^4] // 使用命名空间 use Endroid\QrCode\QrCode; // 创建一个QrCode对象,并设置要生成二维码的文本 $qrCode = new QrCode('https://www.example.com'); // 设置二维码的一些参数(可选) $qrCode->setSize(300); // 设置二维码的大小为300x300像素[^4] $qrCode->setMargin(10); // 设置二维码的边距为10像素 // 将二维码保存为图片文件 $qrCode->writeFile('qrcode.png'); // 保存为qrcode.png文件 // 直接将二维码输出到浏览器上显示 header('Content-Type: ' . $qrCode->getContentType()); // 设置HTTP响应头 echo $qrCode->writeString(); // 输出二维码图片数据 ``` #### 3. 不生成图片文件直接输出二维码 如果不需要将二维码保存为文件,可以直接将其输出到浏览器中。以下是示例代码: ```php <?php require_once 'phpqrcode.php'; // 引入phpqrcode库文件[^3] $value = 'https://www.baidu.com'; // 二维码内容 $errorCorrectionLevel = 'L'; // 容错级别 $matrixPointSize = 5; // 生成图片大小 // 生成二维码图片并直接输出到浏览器 QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize, 2); // 第二个参数为false表示不保存文件[^3] ``` ### 总结 - 如果需要简单的二维码生成方案,可以选择 `phpqrcode` 库。 - 如果需要更灵活的功能(如自定义大小、边距等),可以选择 `Endroid/QrCode` 库[^4]。 - 对于不需要保存文件的情况,可以直接将二维码输出到浏览器中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值