WIN-2308041133\Administrator 2 days ago
parent
commit
c1fb21f9ff
1 changed files with 315 additions and 39 deletions
  1. 315 39
      app/api/controller/user/UserBillController.php

+ 315 - 39
app/api/controller/user/UserBillController.php

@@ -148,6 +148,253 @@ 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([
@@ -200,7 +447,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;
@@ -208,28 +455,31 @@ class UserBillController
                     $avatarUrl = rtrim($siteUrl, '/') . '/' . ltrim($avatarUrl, '/');
                 }
                 $posterWidth = 750;  // 公众号海报标准宽,实际调整
-                $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];  // 注意:原代码两个text都Bold
-                // 昵称宽高 (Bold, 50pt)
+                $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
                 $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,
@@ -245,8 +495,8 @@ class UserBillController
                         'text' => array(
                             array(
                                 'text' => $nickname,
-                                'left' => 300,   // 与头像左边对齐
-                                'top' => 640,    // 头像上方20px
+                                'left' => $nicknameLeft,   // 使用计算出的居中位置
+                                'top' => 640,
                                 'fontPath' => $fontBoldPath,
                                 'fontSize' => 50,
                                 'fontColor' => '40,40,40',
@@ -321,28 +571,31 @@ class UserBillController
                     $avatarUrl = rtrim($siteUrl, '/', '\\') . '/' . ltrim($avatarUrl, '/');
                 }
                 $posterWidth = 783;  // 公众号海报标准宽,实际调整
-                $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];  // 注意:原代码两个text都Bold
-                // 昵称宽高 (Bold, 50pt)
+                $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
                 $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,
@@ -358,8 +611,8 @@ class UserBillController
                         'text' => array(
                             array(
                                 'text' => $nickname,
-                                'left' => 300,   // 与头像左边对齐
-                                'top' => 640,    // 头像上方20px
+                                'left' => $nicknameLeft,   // 使用计算出的居中位置
+                                'top' => 640,
                                 'fontPath' => $fontBoldPath,
                                 'fontSize' => 50,
                                 'fontColor' => '40,40,40',
@@ -396,6 +649,29 @@ class UserBillController
         }
     }
 
+    /**
+     * 增强版的文字居中工具函数
+     * @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
+        ];
+    }
 
     /**
      * 积分记录