|
|
@@ -148,253 +148,6 @@ class UserBillController
|
|
|
* @param Request $request
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function spread_banner(Request $request)
|
|
|
- {
|
|
|
- list($type) = UtilService::getMore([
|
|
|
- ['type', 2],
|
|
|
- ], $request, true);
|
|
|
- $user = $request->user();
|
|
|
- $rootPath = app()->getRootPath();
|
|
|
- try {
|
|
|
- $resRoutine = true;//小程序
|
|
|
- $resWap = true;//公众号
|
|
|
- $siteUrl = sys_config('site_url');
|
|
|
- $routineSpreadBanner = sys_data('routine_spread_banner');
|
|
|
- if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
|
|
|
- if ($type == 1) {
|
|
|
- //小程序
|
|
|
- $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
|
|
|
- $imageInfo = SystemAttachment::getInfo($name, 'name');
|
|
|
- //检测远程文件是否存在
|
|
|
- if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
|
|
|
- $imageInfo = null;
|
|
|
- SystemAttachment::where(['name' => $name])->delete();
|
|
|
- }
|
|
|
- if (!$imageInfo) {
|
|
|
- $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
|
|
|
- if (!$res) return app('json')->fail('二维码生成失败');
|
|
|
- $uploadType = (int)sys_config('upload_type', 1);
|
|
|
- $upload = new Upload($uploadType, [
|
|
|
- 'accessKey' => sys_config('accessKey'),
|
|
|
- 'secretKey' => sys_config('secretKey'),
|
|
|
- 'uploadUrl' => sys_config('uploadUrl'),
|
|
|
- 'storageName' => sys_config('storage_name'),
|
|
|
- 'storageRegion' => sys_config('storage_region'),
|
|
|
- ]);
|
|
|
- $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
|
|
|
- if ($uploadRes === false) {
|
|
|
- return app('json')->fail($upload->getError());
|
|
|
- }
|
|
|
- $imageInfo = $upload->getUploadInfo();
|
|
|
- $imageInfo['image_type'] = $uploadType;
|
|
|
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
|
|
|
- RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
|
|
|
- $urlCode = $imageInfo['dir'];
|
|
|
- } else $urlCode = $imageInfo['att_dir'];
|
|
|
- if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
|
|
|
- $siteUrlHttps = set_http_type($siteUrl, 0);
|
|
|
- $filelink = [
|
|
|
- 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
|
|
|
- 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
|
|
|
- ];
|
|
|
- if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
|
|
|
- if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
|
|
|
- $avatar = $user['avatar'] ?? '';
|
|
|
- if (empty($avatar)) { // 可选:返回错误或用默认头像
|
|
|
- return app('json')->fail('用户头像不存在');
|
|
|
- }
|
|
|
- $avatarUrl = $avatar;
|
|
|
- if (strpos($avatarUrl, 'http') !== 0) {
|
|
|
- $avatarUrl = rtrim($siteUrl, '/') . '/' . ltrim($avatarUrl, '/');
|
|
|
- }
|
|
|
- $posterWidth = 750; // 公众号海报标准宽,实际调整
|
|
|
- $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
|
|
|
- // 昵称宽高 (Bold, 50pt)
|
|
|
- $nickname = $user['nickname'];
|
|
|
- $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
- $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
- $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
- foreach ($routineSpreadBanner as $key => &$item) {
|
|
|
- $posterInfo = '海报生成失败:(';
|
|
|
- $config = array(
|
|
|
- 'image' => array(
|
|
|
-// array( // 圆形头像
|
|
|
-// 'url' => $avatarUrl,
|
|
|
-// 'stream' => 0,
|
|
|
-// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
-// 'top' => 400, // 上移:从640改为500
|
|
|
-// 'right' => 0,
|
|
|
-// 'bottom' => 0,
|
|
|
-// 'width' => 150,
|
|
|
-// 'height' => 150,
|
|
|
-// 'opacity' => 100,
|
|
|
-// 'circle' => true // 圆形蒙版
|
|
|
-// ),
|
|
|
- array(
|
|
|
- 'url' => $urlCode, //二维码资源
|
|
|
- 'stream' => 0,
|
|
|
- 'left' => 195,
|
|
|
- 'top' => 810,
|
|
|
- 'right' => 0,
|
|
|
- 'bottom' => 0,
|
|
|
- 'width' => 360,
|
|
|
- 'height' => 360,
|
|
|
- 'opacity' => 100
|
|
|
- ),
|
|
|
- ),
|
|
|
- 'text' => array(
|
|
|
- array(
|
|
|
- 'text' => $nickname,
|
|
|
- 'left' => 300, // 与头像左边对齐
|
|
|
- 'top' => 640, // 头像上方20px
|
|
|
- 'fontPath' => $fontBoldPath,
|
|
|
- 'fontSize' => 50,
|
|
|
- 'fontColor' => '40,40,40',
|
|
|
- 'angle' => 0,
|
|
|
- ),
|
|
|
- array(
|
|
|
- 'text' => '邀请您加入' . sys_config('site_name'),
|
|
|
- 'left' => 120,
|
|
|
- 'top' => 720,
|
|
|
- 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
|
|
|
- 'fontSize' => 40,
|
|
|
- 'fontColor' => '40,40,40',
|
|
|
- 'angle' => 0,
|
|
|
- )
|
|
|
- ),
|
|
|
- 'background' => $item['pic']
|
|
|
- );
|
|
|
- $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
|
|
|
- if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
|
|
|
- SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
|
|
|
- if ($resRoutine) {
|
|
|
- if ($posterInfo['image_type'] == 1)
|
|
|
- $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
|
|
|
- else
|
|
|
- $item['poster'] = set_http_type($posterInfo['dir'], 0);
|
|
|
- $item['poster'] = str_replace('\\', '/', $item['poster']);
|
|
|
- }
|
|
|
- }
|
|
|
- } else if ($type == 2) {
|
|
|
- //公众号
|
|
|
- $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
|
|
|
- $imageInfo = SystemAttachment::getInfo($name, 'name');
|
|
|
- //检测远程文件是否存在
|
|
|
- if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
|
|
|
- $imageInfo = null;
|
|
|
- SystemAttachment::where(['name' => $name])->delete();
|
|
|
- }
|
|
|
- if (!$imageInfo) {
|
|
|
- try {
|
|
|
- $qr_code = QrcodeService::getForeverQrcode('spread', $user['uid']);
|
|
|
- if (isset($qr_code['url'])) {
|
|
|
- $urlCode = $qr_code['url'];
|
|
|
- } else {
|
|
|
- $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 1);//二维码链接
|
|
|
- $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
|
|
|
- if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
|
|
|
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
|
|
|
- $urlCode = $imageInfo['dir'];
|
|
|
- }
|
|
|
- } catch (\Exception $e) {
|
|
|
- $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 1);//二维码链接
|
|
|
- $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
|
|
|
- if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
|
|
|
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
|
|
|
- $urlCode = $imageInfo['dir'];
|
|
|
- }
|
|
|
- } else $urlCode = $imageInfo['att_dir'];
|
|
|
- if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
|
|
|
- $siteUrl = set_http_type($siteUrl, 1);
|
|
|
- $filelink = [
|
|
|
- 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
|
|
|
- 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
|
|
|
- ];
|
|
|
- if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
|
|
|
- if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
|
|
|
- $avatar = $user['avatar'] ?? '';
|
|
|
- if (empty($avatar)) {
|
|
|
- return app('json')->fail('用户头像不存在');
|
|
|
- }
|
|
|
- $avatarUrl = $avatar;
|
|
|
- if (strpos($avatarUrl, 'http') !== 0) {
|
|
|
- $avatarUrl = rtrim($siteUrl, '/', '\\') . '/' . ltrim($avatarUrl, '/');
|
|
|
- }
|
|
|
- $posterWidth = 783; // 公众号海报标准宽,实际调整
|
|
|
- $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
|
|
|
- // 昵称宽高 (Bold, 50pt)
|
|
|
- $nickname = $user['nickname'];
|
|
|
- $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
- $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
- $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
- foreach ($routineSpreadBanner as $key => &$item) {
|
|
|
- $posterInfo = '海报生成失败:(';
|
|
|
- $config = array(
|
|
|
- 'image' => array(
|
|
|
-// array( // 圆形头像
|
|
|
-// 'url' => $avatarUrl,
|
|
|
-// 'stream' => 0,
|
|
|
-// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
-// 'top' => 400, // 上移:从640改为500
|
|
|
-// 'right' => 0,
|
|
|
-// 'bottom' => 0,
|
|
|
-// 'width' => 150,
|
|
|
-// 'height' => 150,
|
|
|
-// 'opacity' => 100,
|
|
|
-// 'circle' => true // 圆形蒙版
|
|
|
-// ),
|
|
|
- array(
|
|
|
- 'url' => $urlCode, //二维码资源
|
|
|
- 'stream' => 0,
|
|
|
- 'left' => 195,
|
|
|
- 'top' => 810,
|
|
|
- 'right' => 0,
|
|
|
- 'bottom' => 0,
|
|
|
- 'width' => 360,
|
|
|
- 'height' => 360,
|
|
|
- 'opacity' => 100
|
|
|
- ),
|
|
|
- ),
|
|
|
- 'text' => array(
|
|
|
- array(
|
|
|
- 'text' => $nickname,
|
|
|
- 'left' => 300, // 与头像左边对齐
|
|
|
- 'top' => 640, // 头像上方20px
|
|
|
- 'fontPath' => $fontBoldPath,
|
|
|
- 'fontSize' => 50,
|
|
|
- 'fontColor' => '40,40,40',
|
|
|
- 'angle' => 0,
|
|
|
- ),
|
|
|
- array(
|
|
|
- 'text' => '邀请您加入' . sys_config('site_name'),
|
|
|
- 'left' => 120,
|
|
|
- 'top' => 720,
|
|
|
- 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
|
|
|
- 'fontSize' => 40,
|
|
|
- 'fontColor' => '40,40,40',
|
|
|
- 'angle' => 0,
|
|
|
- )
|
|
|
- ),
|
|
|
- 'background' => $item['pic']
|
|
|
- );
|
|
|
- $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
|
|
|
- if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
|
|
|
- SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
|
|
|
- if ($resWap) {
|
|
|
- if ($posterInfo['image_type'] == 1)
|
|
|
- $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
|
|
|
- else
|
|
|
- $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
|
|
|
- else return app('json')->fail('生成图片失败');
|
|
|
- } catch (\Exception $e) {
|
|
|
- @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
|
|
|
- return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
|
|
|
- }
|
|
|
- }
|
|
|
// public function spread_banner(Request $request)
|
|
|
// {
|
|
|
// list($type) = UtilService::getMore([
|
|
|
@@ -447,7 +200,7 @@ class UserBillController
|
|
|
// if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
|
|
|
// if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
|
|
|
// $avatar = $user['avatar'] ?? '';
|
|
|
-// if (empty($avatar)) {
|
|
|
+// if (empty($avatar)) { // 可选:返回错误或用默认头像
|
|
|
// return app('json')->fail('用户头像不存在');
|
|
|
// }
|
|
|
// $avatarUrl = $avatar;
|
|
|
@@ -455,31 +208,28 @@ class UserBillController
|
|
|
// $avatarUrl = rtrim($siteUrl, '/') . '/' . ltrim($avatarUrl, '/');
|
|
|
// }
|
|
|
// $posterWidth = 750; // 公众号海报标准宽,实际调整
|
|
|
-// $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
|
|
|
+// $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
|
|
|
+// // 昵称宽高 (Bold, 50pt)
|
|
|
// $nickname = $user['nickname'];
|
|
|
-//
|
|
|
+// $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
+// $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
+// $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
// foreach ($routineSpreadBanner as $key => &$item) {
|
|
|
// $posterInfo = '海报生成失败:(';
|
|
|
-//
|
|
|
-// // 计算昵称文字的宽度和居中位置
|
|
|
-// $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
-// $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
-// $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
-//
|
|
|
// $config = array(
|
|
|
// 'image' => array(
|
|
|
-//// array( // 圆形头像
|
|
|
-//// 'url' => $avatarUrl,
|
|
|
-//// 'stream' => 0,
|
|
|
-//// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
-//// 'top' => 400, // 上移:从640改为500
|
|
|
-//// 'right' => 0,
|
|
|
-//// 'bottom' => 0,
|
|
|
-//// 'width' => 150,
|
|
|
-//// 'height' => 150,
|
|
|
-//// 'opacity' => 100,
|
|
|
-//// 'circle' => true // 圆形蒙版
|
|
|
-//// ),
|
|
|
+//// array( // 圆形头像
|
|
|
+//// 'url' => $avatarUrl,
|
|
|
+//// 'stream' => 0,
|
|
|
+//// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
+//// 'top' => 400, // 上移:从640改为500
|
|
|
+//// 'right' => 0,
|
|
|
+//// 'bottom' => 0,
|
|
|
+//// 'width' => 150,
|
|
|
+//// 'height' => 150,
|
|
|
+//// 'opacity' => 100,
|
|
|
+//// 'circle' => true // 圆形蒙版
|
|
|
+//// ),
|
|
|
// array(
|
|
|
// 'url' => $urlCode, //二维码资源
|
|
|
// 'stream' => 0,
|
|
|
@@ -495,8 +245,8 @@ class UserBillController
|
|
|
// 'text' => array(
|
|
|
// array(
|
|
|
// 'text' => $nickname,
|
|
|
-// 'left' => $nicknameLeft, // 使用计算出的居中位置
|
|
|
-// 'top' => 640,
|
|
|
+// 'left' => 300, // 与头像左边对齐
|
|
|
+// 'top' => 640, // 头像上方20px
|
|
|
// 'fontPath' => $fontBoldPath,
|
|
|
// 'fontSize' => 50,
|
|
|
// 'fontColor' => '40,40,40',
|
|
|
@@ -571,31 +321,28 @@ class UserBillController
|
|
|
// $avatarUrl = rtrim($siteUrl, '/', '\\') . '/' . ltrim($avatarUrl, '/');
|
|
|
// }
|
|
|
// $posterWidth = 783; // 公众号海报标准宽,实际调整
|
|
|
-// $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
|
|
|
+// $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
|
|
|
+// // 昵称宽高 (Bold, 50pt)
|
|
|
// $nickname = $user['nickname'];
|
|
|
-//
|
|
|
+// $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
+// $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
+// $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
// foreach ($routineSpreadBanner as $key => &$item) {
|
|
|
// $posterInfo = '海报生成失败:(';
|
|
|
-//
|
|
|
-// // 计算昵称文字的宽度和居中位置
|
|
|
-// $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
|
|
|
-// $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
|
|
|
-// $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
|
|
|
-//
|
|
|
// $config = array(
|
|
|
// 'image' => array(
|
|
|
-//// array( // 圆形头像
|
|
|
-//// 'url' => $avatarUrl,
|
|
|
-//// 'stream' => 0,
|
|
|
-//// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
-//// 'top' => 400, // 上移:从640改为500
|
|
|
-//// 'right' => 0,
|
|
|
-//// 'bottom' => 0,
|
|
|
-//// 'width' => 150,
|
|
|
-//// 'height' => 150,
|
|
|
-//// 'opacity' => 100,
|
|
|
-//// 'circle' => true // 圆形蒙版
|
|
|
-//// ),
|
|
|
+//// array( // 圆形头像
|
|
|
+//// 'url' => $avatarUrl,
|
|
|
+//// 'stream' => 0,
|
|
|
+//// 'left' => 300, // 居中位置:(750-150)/2 = 300
|
|
|
+//// 'top' => 400, // 上移:从640改为500
|
|
|
+//// 'right' => 0,
|
|
|
+//// 'bottom' => 0,
|
|
|
+//// 'width' => 150,
|
|
|
+//// 'height' => 150,
|
|
|
+//// 'opacity' => 100,
|
|
|
+//// 'circle' => true // 圆形蒙版
|
|
|
+//// ),
|
|
|
// array(
|
|
|
// 'url' => $urlCode, //二维码资源
|
|
|
// 'stream' => 0,
|
|
|
@@ -611,8 +358,8 @@ class UserBillController
|
|
|
// 'text' => array(
|
|
|
// array(
|
|
|
// 'text' => $nickname,
|
|
|
-// 'left' => $nicknameLeft, // 使用计算出的居中位置
|
|
|
-// 'top' => 640,
|
|
|
+// 'left' => 300, // 与头像左边对齐
|
|
|
+// 'top' => 640, // 头像上方20px
|
|
|
// 'fontPath' => $fontBoldPath,
|
|
|
// 'fontSize' => 50,
|
|
|
// 'fontColor' => '40,40,40',
|
|
|
@@ -648,31 +395,231 @@ class UserBillController
|
|
|
// return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
|
|
|
// }
|
|
|
// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 增强版的文字居中工具函数
|
|
|
-// * @param string $text 文字内容
|
|
|
-// * @param string $fontPath 字体文件路径
|
|
|
-// * @param int $fontSize 字体大小
|
|
|
-// * @param int $containerWidth 容器宽度
|
|
|
-// * @return array 包含left位置和文字宽度的数组
|
|
|
-// */
|
|
|
-// function getTextCenterPosition($text, $fontPath, $fontSize, $containerWidth) {
|
|
|
-// // 获取文字边界框
|
|
|
-// $box = imagettfbbox($fontSize, 0, $fontPath, $text);
|
|
|
-//
|
|
|
-// // 计算文字宽度
|
|
|
-// $textWidth = abs($box[4] - $box[6]);
|
|
|
-//
|
|
|
-// // 计算居中位置
|
|
|
-// $centerLeft = ($containerWidth - $textWidth) / 2;
|
|
|
-//
|
|
|
-// return [
|
|
|
-// 'left' => $centerLeft,
|
|
|
-// 'width' => $textWidth
|
|
|
-// ];
|
|
|
-// }
|
|
|
|
|
|
+ public static function setSharePoster($config = array(), $path)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $imageDefault = array(
|
|
|
+ 'left' => 0,
|
|
|
+ 'top' => 0,
|
|
|
+ 'right' => 0,
|
|
|
+ 'bottom' => 0,
|
|
|
+ 'width' => 100,
|
|
|
+ 'height' => 100,
|
|
|
+ 'opacity' => 100,
|
|
|
+ 'circle' => false // 是否裁剪为圆形
|
|
|
+ );
|
|
|
+ $textDefault = array(
|
|
|
+ 'text' => '',
|
|
|
+ 'left' => 0,
|
|
|
+ 'top' => 0,
|
|
|
+ 'fontSize' => 32, //字号
|
|
|
+ 'fontColor' => '255,255,255', //字体颜色
|
|
|
+ 'angle' => 0,
|
|
|
+ );
|
|
|
+
|
|
|
+ $background = $config['background'];//海报最底层得背景
|
|
|
+ if (substr($background, 0, 1) === '/') {
|
|
|
+ $background = substr($background, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查背景图片是否存在
|
|
|
+ if (!file_exists($background) && !filter_var($background, FILTER_VALIDATE_URL)) {
|
|
|
+ throw new \Exception('背景图片不存在: ' . $background);
|
|
|
+ }
|
|
|
+
|
|
|
+ $backgroundInfo = getimagesize($background);
|
|
|
+ if ($backgroundInfo === false) {
|
|
|
+ throw new \Exception('无法获取背景图片信息');
|
|
|
+ }
|
|
|
+
|
|
|
+ $backgroundContent = file_get_contents($background);
|
|
|
+ if ($backgroundContent === false) {
|
|
|
+ throw new \Exception('无法读取背景图片');
|
|
|
+ }
|
|
|
+
|
|
|
+ $background = imagecreatefromstring($backgroundContent);
|
|
|
+ if ($background === false) {
|
|
|
+ throw new \Exception('无法创建背景图片资源');
|
|
|
+ }
|
|
|
+
|
|
|
+ $backgroundWidth = $backgroundInfo[0]; //背景宽度
|
|
|
+ $backgroundHeight = $backgroundInfo[1]; //背景高度
|
|
|
+
|
|
|
+ // 创建主图像,支持透明度
|
|
|
+ $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
|
|
|
+
|
|
|
+ // 设置透明背景
|
|
|
+ imagealphablending($imageRes, false);
|
|
|
+ imagesavealpha($imageRes, true);
|
|
|
+ $transparent = imagecolorallocatealpha($imageRes, 255, 255, 255, 127);
|
|
|
+ imagefilledrectangle($imageRes, 0, 0, $backgroundWidth, $backgroundHeight, $transparent);
|
|
|
+
|
|
|
+ // 复制背景图片
|
|
|
+ imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
|
|
|
+
|
|
|
+ // 恢复混合模式
|
|
|
+ imagealphablending($imageRes, true);
|
|
|
+
|
|
|
+ if (!empty($config['image'])) {
|
|
|
+ foreach ($config['image'] as $key => $val) {
|
|
|
+ $val = array_merge($imageDefault, $val);
|
|
|
+
|
|
|
+ // 检查图片URL是否存在
|
|
|
+ if (empty($val['url'])) {
|
|
|
+ continue; // 跳过空的图片URL
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理URL:如果是相对路径,转换为绝对路径
|
|
|
+ $imageUrl = $val['url'];
|
|
|
+ if (!filter_var($imageUrl, FILTER_VALIDATE_URL) && file_exists($imageUrl)) {
|
|
|
+ // 如果是本地文件路径,直接使用
|
|
|
+ $imageContent = file_get_contents($imageUrl);
|
|
|
+ } else {
|
|
|
+ // 尝试获取远程图片
|
|
|
+ $imageContent = @file_get_contents($imageUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($imageContent === false) {
|
|
|
+ // 如果无法获取图片,跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $res = imagecreatefromstring($imageContent);
|
|
|
+ if ($res === false) {
|
|
|
+ // 如果无法创建图片资源,跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $resWidth = imagesx($res);
|
|
|
+ $resHeight = imagesy($res);
|
|
|
+
|
|
|
+ // 如果是圆形,创建圆形图片
|
|
|
+ if ($val['circle']) {
|
|
|
+ // 创建一个支持透明的画布
|
|
|
+ $canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+
|
|
|
+ // 设置透明背景
|
|
|
+ imagealphablending($canvas, false);
|
|
|
+ imagesavealpha($canvas, true);
|
|
|
+ $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
|
|
|
+ imagefilledrectangle($canvas, 0, 0, $val['width'], $val['height'], $transparent);
|
|
|
+
|
|
|
+ // 将原图缩放到指定大小
|
|
|
+ $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+ imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
+
|
|
|
+ // 创建圆形:只复制圆形区域内的像素
|
|
|
+ $centerX = $val['width'] / 2;
|
|
|
+ $centerY = $val['height'] / 2;
|
|
|
+ $radius = min($val['width'], $val['height']) / 2;
|
|
|
+
|
|
|
+ // 恢复混合模式
|
|
|
+ imagealphablending($canvas, true);
|
|
|
+
|
|
|
+ for ($x = 0; $x < $val['width']; $x++) {
|
|
|
+ for ($y = 0; $y < $val['height']; $y++) {
|
|
|
+ $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
|
|
|
+ if ($distance <= $radius) {
|
|
|
+ $pixelColor = imagecolorat($sourceResized, $x, $y);
|
|
|
+ imagesetpixel($canvas, $x, $y, $pixelColor);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ imagedestroy($sourceResized);
|
|
|
+ imagedestroy($res);
|
|
|
+ } else {
|
|
|
+ // 非圆形,按原逻辑处理
|
|
|
+ $canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+
|
|
|
+ $bgColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
+ imagefill($canvas, 0, 0, $bgColor);
|
|
|
+
|
|
|
+ imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
+
|
|
|
+ imagedestroy($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
|
|
|
+ $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
|
|
|
+
|
|
|
+ // 合并到主图像
|
|
|
+ if ($val['circle']) {
|
|
|
+ // 对于圆形图片,使用imagecopy以支持透明度
|
|
|
+ imagecopy($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height']);
|
|
|
+ } else {
|
|
|
+ // 对于普通图片,使用imagecopymerge
|
|
|
+ imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
|
|
|
+ }
|
|
|
+
|
|
|
+ imagedestroy($canvas);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($config['text']) && !empty($config['text'])) {
|
|
|
+ foreach ($config['text'] as $key => $val) {
|
|
|
+ $val = array_merge($textDefault, $val);
|
|
|
+ list($R, $G, $B) = explode(',', $val['fontColor']);
|
|
|
+ $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
|
|
|
+ $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
|
|
|
+ $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
|
|
|
+ imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 输出图片前再次确保保存alpha通道
|
|
|
+ imagesavealpha($imageRes, true);
|
|
|
+
|
|
|
+ ob_start();
|
|
|
+ imagejpeg($imageRes, null, 90); // 使用较高质量
|
|
|
+ imagedestroy($imageRes);
|
|
|
+ $res = ob_get_contents();
|
|
|
+ ob_end_clean();
|
|
|
+
|
|
|
+ $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
|
|
|
+ $uploadType = (int)sys_config('upload_type', 1);
|
|
|
+ $upload = new Upload($uploadType, [
|
|
|
+ 'accessKey' => sys_config('accessKey'),
|
|
|
+ 'secretKey' => sys_config('secretKey'),
|
|
|
+ 'uploadUrl' => sys_config('uploadUrl'),
|
|
|
+ 'storageName' => sys_config('storage_name'),
|
|
|
+ 'storageRegion' => sys_config('storage_region'),
|
|
|
+ ]);
|
|
|
+ $res = $upload->to($path)->validate()->stream($res, $key);
|
|
|
+ if ($res === false) {
|
|
|
+ return $upload->getError();
|
|
|
+ } else {
|
|
|
+ $info = $upload->getUploadInfo();
|
|
|
+ $info['image_type'] = $uploadType;
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
|
|
|
+ return '生成图片时,系统错误: ' . $e->getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 辅助函数:带透明度的图像复制
|
|
|
+ * 注意:这个函数在PHP 5.5+中可能不需要,因为imagecopy已经支持透明度
|
|
|
+ */
|
|
|
+ function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
|
|
|
+ // 创建一个临时图像
|
|
|
+ $tmp = imagecreatetruecolor($src_w, $src_h);
|
|
|
+
|
|
|
+ // 复制源图像到临时图像
|
|
|
+ imagecopy($tmp, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
|
|
|
+
|
|
|
+ // 合并源图像到临时图像
|
|
|
+ imagecopy($tmp, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
|
|
|
+
|
|
|
+ // 合并临时图像到目标图像
|
|
|
+ imagecopymerge($dst_im, $tmp, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
|
|
|
+
|
|
|
+ // 销毁临时图像
|
|
|
+ imagedestroy($tmp);
|
|
|
+ }
|
|
|
/**
|
|
|
* 积分记录
|
|
|
* @param Request $request
|