# diy-qrcode **Repository Path**: inkccnet/diy-qrcode ## Basic Information - **Project Name**: diy-qrcode - **Description**: PHP生成二维码海报,支持多模板。 - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 56 - **Created**: 2026-06-06 - **Last Updated**: 2026-06-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DIY QR Code - 二维码海报生成器 基于 PHP 8 + GD 库的二维码海报生成工具,支持多模板皮肤,可将二维码叠加到海报图片上,并支持自定义文字输出。 ## 环境要求 - PHP >= 8.0 - GD 扩展(`ext-gd`) - Composer ## 安装 ```bash composer require mqycn/diy-qrcode ``` 或者克隆后手动安装依赖: ```bash git clone https://gitee.com/mqycn/diy-qrcode.git cd diy-qrcode composer install ``` ## 快速开始 ### 1. 浏览器直接输出二维码海报 ```php require_once 'vendor/autoload.php'; use DiyQrcode\Generator; $qrcode = new Generator(['skin' => 'skin1']); $qrcode->setKey('https://example.com'); $qrcode->output(); // 直接输出图片到浏览器 ``` ### 2. 保存二维码海报到文件 ```php $qrcode = new Generator([ 'skin' => 'skin2', 'x' => 228, 'y' => 77, 'w' => 88, 'h' => 88, ]); $qrcode->setKey('INVITE_CODE_001'); $result = $qrcode->save('./qrimg/poster.png'); // $result = ['filename' => './qrimg/poster.png', 'url' => 'http://...'] ``` ### 3. 通过 URL 生成(main.php) ``` main.php?skin=skin1&key=BASE64_ENCODED_TEXT ``` 如需返回 JSON 格式结果: ``` main.php?skin=skin1&key=BASE64_ENCODED_TEXT&response_type=json ``` ## API 参考 ### Generator 类 ```php namespace DiyQrcode; class Generator { public function __construct(array $config = []); public function setConf(array $config = []): void; public function setKey(string $key = ''): void; public function output(?string $file_name = null): ?array; public function save(string $file_name): array; } ``` #### 构造参数 / `setConf()` 参数 | 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | `skin` | string | `'skin1'` | 模板名称,对应 `qrcode.{skin}/` 目录 | | `background` | string | `'background.jpg'` | 背景图路径,支持 `[SKIN]` 占位符 | | `level` | string | `'M'` | 二维码纠错级别:`L`、`M`、`Q`、`H` | | `matrix` | int | `6` | 二维码模块像素大小,1-10 | | `type` | string | `'png'` | 输出格式:`png`、`jpg` | | `x` | int | `0` | 二维码在背景图上的 X 坐标 | | `y` | int | `0` | 二维码在背景图上的 Y 坐标 | | `w` | int | `100` | 二维码在背景图上的宽度 | | `h` | int | `100` | 二维码在背景图上的高度 | | `template` | string | - | 落地页 URL 模板,支持 `[KEY]`、`[WEB_ROOT]`、`[WEB_PATH]`、`[WEB_URI]` | | `font` | string | `''` | TrueType 字体路径,支持 `[SKIN]` 占位符 | | `text` | string\|false | `false` | 叠加文字内容,支持 `[KEY]` 占位符;`false` 则不显示 | | `textsize` | int | `12` | 文字大小,5-50 | | `textx` | int | `0` | 文字在背景图上的 X 坐标 | | `texty` | int | `0` | 文字在背景图上的 Y 坐标(字体基线) | | `textcolor` | string | `'#000000'` | 文字颜色,十六进制格式 | ## 增加模板 模板以 `qrcode.` 为前缀的文件夹形式存放。 ### 1. 创建模板目录 ``` qrcode.your_template/ ├── config.php # 模板配置文件(必须) ├── background.jpg # 海报背景图 └── your-font.ttf # 字体文件(如需文字叠加) ``` ### 2. 编写 config.php ```php 'L', // 纠错级别:L / M / Q / H 'matrix' => 6, // 模块像素大小,1-10 'type' => 'png', // 输出格式:png / jpg // ── 图片文件([SKIN] 会被替换为 ./qrcode.{skin}/)── 'background' => '[SKIN]demo.png', // ── 落地页链接 ── // [KEY] → 二维码正文 // [WEB_ROOT] → http://your-domain.com/ // [WEB_PATH] → 安装路径/ // [WEB_URI] → http://your-domain.com/安装路径/ 'template' => '[WEB_URI]test.php?qrcode=[KEY]&skin=your_template', // ── 二维码位置和尺寸 ── 'x' => 228, 'y' => 77, 'w' => 88, 'h' => 88, // ── 文字叠加(可选,不需要则删除以下全部)── 'font' => '[SKIN]your-font.ttf', 'text' => '[KEY]', 'textsize' => 14, 'textx' => 138, 'texty' => 369, 'textcolor' => '#FFFFFF', ]; ``` ### 3. 注册白名单 在 `main.php` 中将新模板名加入白名单: ```php $allowed_skins = ['skin1', 'skin2', 'skin3', 'your_template']; ``` ## 内置模板 ### 样式一(skin1) 纯背景 + 二维码 ### 样式二(skin2) 带 logo 风格海报 + 二维码 ### 样式三(skin3) 海报 + 二维码 + 文字叠加 ## 在线演示 测试地址:`http://您的域名/安装路径/test.php` ## 依赖 - [chillerlan/php-qrcode](https://github.com/chillerlan/php-qrcode) - 现代化 PHP QR Code 生成库 ## License MIT