wzq před 5 roky
rodič
revize
2bdd76f41f

+ 71 - 38
app/Helper/helper.php

@@ -17,6 +17,21 @@ if ( ! function_exists('config_path'))
     }
 }
 
+if (! function_exists('public_path')) {
+    /**
+     * Get the path to the public folder.
+     *
+     * @param  string  $path
+     * @return string
+     */
+    function public_path($path = '')
+    {
+        return app()->basePath() . '/public' . ($path ? '/' . $path : $path);
+    }
+
+
+}
+
 /**
  * 参数签名校验
  * @param array $params
@@ -59,46 +74,64 @@ function verifySign($sign, $params, $secret_key)
 
 }
 
-     function jsonSuccess($data = [], $msg = "成功")
-    {
-        $response = array(
-            'code' => 0,
-            'msg' => $msg,
-            'data' => []
-        );
-        if ($data) {
-            if (is_array($data)) {
-                //带有分页格式转换
-                if (isset($data['meta'])) {
-                    // 更改元数据格式,全部包含在data下
-                    $temp = array(
-                        'data' => array(
-                            'data' => $data['data'],
-                            'pagination' => $data['meta']['pagination']
-                        )
-                    );
-                    $response = array_merge($response, $temp);
-                } elseif(isset($data['data'])) {
-                    $response = array_merge($response, $data);
-                }else{
-                    $temp = array(
-                        'data' => $data
-                    );
-                    $response = array_merge($response, $temp);
-                }
-            } else {
-                $response['data'] = $data;
+ function jsonSuccess($data = [], $msg = "成功")
+{
+    $response = array(
+        'code' => 0,
+        'msg' => $msg,
+        'data' => []
+    );
+    if ($data) {
+        if (is_array($data)) {
+            //带有分页格式转换
+            if (isset($data['meta'])) {
+                // 更改元数据格式,全部包含在data下
+                $temp = array(
+                    'data' => array(
+                        'data' => $data['data'],
+                        'pagination' => $data['meta']['pagination']
+                    )
+                );
+                $response = array_merge($response, $temp);
+            } elseif(isset($data['data'])) {
+                $response = array_merge($response, $data);
+            }else{
+                $temp = array(
+                    'data' => $data
+                );
+                $response = array_merge($response, $temp);
             }
+        } else {
+            $response['data'] = $data;
         }
-        return $response;
     }
+    return $response;
+}
+
+function jsonError($msg)
+{
+    $response = array(
+        'code' => 1,
+        'msg' => $msg,
+        'data' => ""
+    );
+    return $response;
+}
 
-        function jsonError($msg)
-        {
-            $response = array(
-                'code' => 1,
-                'msg' => $msg,
-                'data' => ""
-            );
-            return $response;
+function http($url, $param, $method = 'post')
+{
+    try {
+        $client = new \GuzzleHttp\Client();
+        $response = $client->request($method, $url, $param);
+        $result = json_decode($response->getBody()->getContents(), true);
+        \Illuminate\Support\Facades\Log::debug('url:'.$url.'param:'.json_encode($param).'response:'.json_encode($result));
+        if (isset($result['code']) && $result['code'] == 0) {
+            return $result['data'];
+        } else {
+            return [];
         }
+    } catch (\Exception $exception) {
+        return [];
+    }
+
+}

+ 45 - 0
app/Http/Controllers/PosterController.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/20
+ * Time: 16:34
+ */
+namespace App\Http\Controllers;
+
+use App\Repositories\PosterRepository;
+use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Http\Request;
+use Intervention\Image\ImageManagerStatic;
+
+class PosterController extends Controller
+{
+    use UserTrait;
+    public function __construct()
+    {
+    }
+
+    public function post(Request $request)
+    {
+//        return ImageManagerStatic::make(public_path('/image/post/main.png'))->reponse('png');
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+
+        $userInfo = $this->getUserInfo();
+        if(!$userInfo){
+            return jsonError('获取用户信息失败');
+        }
+
+        // 合成基本图
+        $main = public_path('/image/post/main.png');
+        $imageRepository = new PosterRepository($main);
+
+        $url = $imageRepository->post($userInfo, $request['id']);
+        return $url;
+    }
+}

+ 48 - 0
app/Repositories/PosterRepository.php

@@ -0,0 +1,48 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/20
+ * Time: 16:50
+ */
+namespace App\Repositories;
+
+use Faker\Provider\Image;
+use Intervention\Image\ImageManagerStatic;
+use Illuminate\Support\Facades\Storage;
+
+class PosterRepository
+{
+    public $main;
+
+    public function __construct($mainImg)
+    {
+        if (extension_loaded('imagick')) {
+            ImageManagerStatic::configure(['driver'=>'imagick']);
+        }
+        $this->main = ImageManagerStatic::make($mainImg);
+        return $this->main->encoded;
+    }
+
+    /**
+     * 分享帖子生成海报
+     */
+    public function post($userInfo, $id)
+    {
+//        return $this->main->response();
+        // 设置头像
+        if(!$userInfo['avatar']){
+            $userInfo['avatar'] = 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132';
+        }
+        $avatarurl = ImageManagerStatic::make($userInfo['avatar'])->fit(80, 80);
+        $image = $this->main->insert($avatarurl, 'left-top', 40, 309*2);
+        $path = '/public/post'.$id.'.png';
+        $image->save($path);
+        //$url = config('filesystems.disks.oss.uri').$path;
+
+        // 存储图片
+        $url = Storage::put($path, $image->basePath());
+
+        return $url;
+    }
+}

+ 28 - 0
app/Traits/UserTrait.php

@@ -0,0 +1,28 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/5/5
+ * Time: 17:11
+ */
+namespace App\Traits;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+trait UserTrait
+{
+
+    public function getUserInfo() {
+        try {
+            $sign = generateSign([], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/user/userInfo';
+            $array = [
+                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array);
+        } catch (\Exception $e) {
+            return [];
+        }
+
+    }
+}

+ 1 - 0
composer.json

@@ -18,6 +18,7 @@
         "multilinguals/apollo-client": "^0.1.2",
         "predis/predis": "^1.1",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "guzzlehttp/guzzle": "^6.3",
         "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {

binární
public/font/PingFang Bold.ttf


binární
public/font/PingFang Medium.ttf


binární
public/font/PingFang Regular.ttf


binární
public/image/post/logo.png


binární
public/image/post/main.png


binární
public/image/post/mask.png


+ 2 - 0
routes/api.php

@@ -21,6 +21,8 @@ $api->version('v1', [
     $api->group(['middleware' => 'chxq_jwt_auth'], function ($api) {
         $api->post('upload', 'UploadController@uploadImage');
         $api->post('multi_upload', 'UploadController@uploadImages');
+
+        $api->get('poster/post', 'PosterController@post');
     });
     //登录+验签
     $api->group(['middleware' => ['chxq_jwt_auth','chxq_sign']], function ($api) {