# 生成海報
* * * * *
生成海報之前需要準備二維碼和海報背景圖。
二維碼可參考上面的章節生成,并將路徑保存到相應的數據表中。
海報可以根據實際情況處理,可以是靜態固定的背景圖片也可以是設置上傳的背景圖。
此處為演示方面暫時用靜態圖片演示海報生成功能。
上層代碼如下
```
/**
* 生成海報
*/
public function demoPoster()
{
// windows
$poster_data = create_poster(
"D:/xampp/htdocs/OneBase/public/upload/extend/qrcode/e9ff27b4d969cfad54b5388c381e2022.png",
"D:/xampp/htdocs/OneBase/public/upload/extend/poster/poster_bg.jpg",
200,
[200,100]
);
// linux
/*
$poster_data = create_poster(
"./upload/extend/qrcode/e9ff27b4d969cfad54b5388c381e2022.png",
"./upload/extend/poster/poster_bg.jpg",
200,
[200,100]
);
*/
dump($poster_data);
}
```
海報生成函數如下 函數所在文件(app/extend.php)
```
/**
* 生成海報
* @param staring $qrcode_path 海報二維碼圖片路徑
* @param staring $poster_bg_path 海報背景圖片路徑
* @param int $qrcode_size 二維碼大小
* @param array $location 二維碼位置,數組中包含2個值,分別代表x y距離,通過此參數調整二維碼在海報中的位置
*/
function create_poster($qrcode_path = '', $poster_bg_path = '', $qrcode_size = 200, $location = [0,0])
{
// 海報目錄路徑
$poster_path = './upload/extend/poster/';
// 將二維碼生成縮略圖
if (file_exists($qrcode_path) && file_exists($poster_bg_path)) {
$qrcode_tmp_file = md5($qrcode_path).'_qrcode.jpg';
$qrcode_tmp_file_path = $poster_path . $qrcode_tmp_file;
$image_thumb = \think\Image::open($qrcode_path);
$image_thumb->thumb($qrcode_size, $qrcode_size,\think\Image::THUMB_SCALING)->save($qrcode_tmp_file_path);
$poster_file_name = md5($qrcode_path).'_poster.jpg';
$poster_file_path = $poster_path .$poster_file_name;
$image_water = \think\Image::open($poster_bg_path);
$image_water->water($qrcode_tmp_file_path, $location)->save($poster_file_path);
@unlink($qrcode_tmp_file_path);
$return_data['name'] = $poster_file_name;
$return_data['path'] = $poster_file_path;
return $return_data;
} else {
return false;
}
}
```
執行結果返回了海報的文件名稱和保存的路徑
```
array(2) {
["name"] => string(43) "847cc36b83d8c1fe3f97db5953e9fc46_poster.jpg"
["path"] => string(66) "./upload/extend/poster/847cc36b83d8c1fe3f97db5953e9fc46_poster.jpg"
}
```
效果圖

create_poster函數僅演示海報生成,若滿足不了需求,比如您需要在海報中寫入會員昵稱。
可修改create_poster函數,調用$image->text()方法將文字寫入海報。
- 序言
- 基礎
- 安裝環境
- 安裝演示
- 規范
- 目錄
- 介紹
- 后臺介紹
- 后臺首頁
- 會員管理
- 系統管理
- 系統設置與配置管理
- 菜單管理
- 系統回收站
- 服務管理
- 插件管理
- 文章管理
- 接口管理
- 優化維護
- SEO管理
- 數據庫
- 文件清理
- 行為日志
- 執行記錄
- 統計分析
- 接口介紹
- 接口文檔
- 錯誤碼設計
- Token介紹
- 前臺介紹
- 架構
- 架構總覽
- 生命周期
- 入口文件
- 模塊設計
- 依賴注入
- 控制器架構
- 邏輯架構
- 驗證架構
- 服務架構
- 模型架構
- 行為架構
- 插件架構
- 配置
- 配置介紹
- 配置加載
- 配置擴展
- 請求
- 請求信息
- 日志
- 后臺行為日志
- 系統執行日志
- 框架日志
- 數據
- 數據庫設計
- 數據字典
- 數據庫操作
- 事務控制
- 混合操作
- 實戰
- 控制器
- 邏輯與驗證
- 視圖與模型
- 插件研發
- 服務研發
- 接口研發
- 雜項
- 數據導入導出
- 二維碼條形碼
- 郵件發送
- 云存儲服務
- 支付服務
- 短信服務
- 微信分享
- 生成海報
- 聊天室
- PJAX
- Demo
- Widget
- 附錄
- 常量參考
- 配置參考
- 函數參考
- 進階
- Redis
- 自動緩存
- 全自動緩存
- 索引
- 數據簽名
- 全自動事務
- 隊列