laravel後端生成微信小程式海報

wongvio發表於2022-09-16

先答為啥不用canvas:
1、canvas相容性讓你除錯到懷疑人生;
2、後端生成一次可以複用;
3、因為裡面有小程式碼,前端生成會暴露key和secret,如果你要後端生成token又傳給前端,不如直接後端搞完算了
4、我沒有可以任意折磨的前端朋友

前端你用啥都可以,只要能接收json,上效果:

上程式碼:

<?php

namespace App\Http\Controllers\Api;

use App\Models\Topic;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Http;

class ShareController extends Controller
{
    public function getWxMiniAppCode(Request $request, Topic $topic)
    {

        $topic = Topic::find($request->topic_id);

        //是否存在目錄,不存在就新建一個
        $path = public_path('uploads/topic_share_wxmini_qrcode/' . $topic->id . '/');

        if (!is_dir($path)){

            $mkdir = mkdir(iconv("UTF-8", "GBK", $path),0755,true);

            $invit_path = '/pages/article/article?TopicId=' . $topic->id . '&type=sharewx';

            $uniBg = public_path('/static/qrcode.jpg');

            $uniBgBottom = public_path('/static/qrcode_bottom.jpg');

            $topicImg = $topic->cover;

            //獲取二維碼
            $miniProgram = \EasyWeChat::miniProgram();

            $data = $miniProgram->app_code->get($invit_path, [
                'width' => '280', //最小就是280,設定再小伺服器也不會返回
            ]);

            $empty_code = public_path('uploads/topic_share_wxmini_qrcode/sharecode.png');

            $empty_share = public_path('uploads/topic_share_wxmini_qrcode/share.jpg');

            if ($data instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $filename = $data->saveAs(public_path('uploads/topic_share_wxmini_qrcode/' . $topic->id . '/'), 'sharecode.png');
            }

            //獲取二維碼,並重新縮放正確比例
            $qrImg = public_path('/uploads/topic_share_wxmini_qrcode/' . $topic->id . '/sharecode.png');

            $qrImgResize = Image::make($qrImg);

            $qrImgResize->widen(190);

            //獲取主圖寬高,並重新縮放正確比例
            $topicImgResize = Image::make($topicImg);

            $topicImgResize->widen(913);

            //開始拼接圖片
            $image = Image::canvas(1080, 1622);

            $image->insert($uniBg, 'top-left', 0, 0);

            $image->insert($topicImgResize, 'top-left', 84, 74);

            $image->insert($uniBgBottom, 'top-left', 0, 1103);

            $image->insert($qrImgResize, 'top-left', 264, 1378);

            $text = $topic->title ? $topic->title : $topic->auto_title;

            //如果文字超出19個字元,就擷取後拼接換行
            $textLength = Str::length($text);

            //單行文字數量限制
            $textNum = 21;

            if($textLength < $textNum) {

                $image->text($text, 80, 1170, function($font) {
                    $font->file(base_path().'/public/font/msyh.ttf');
                    $font->size(46);
                    $font->color('#333');
                    $font->align('left');
                    $font->valign('top');
                    $font->angle(0);
                });

            } else {

                $image->text(Str::substr($text, 0, $textNum), 80, 1170, function($font) {
                    $font->file(base_path().'/public/font/msyh.ttf');
                    $font->size(46);
                    $font->color('#333');
                    $font->align('left');
                    $font->valign('top');
                    $font->angle(0);
                });

                $image->text(Str::substr($text, $textNum, $textLength <= 18 ? $textLength : 18), 80, 1240, function($font) {
                    $font->file(base_path().'/public/font/msyh.ttf');
                    $font->size(46);
                    $font->color('#333');
                    $font->align('left');
                    $font->valign('top');
                    $font->angle(0);
                });

            }

            $image->save(public_path('uploads/topic_share_wxmini_qrcode/' . $topic->id . '/share.jpg'));

            $res_path = '/uploads/topic_share_wxmini_qrcode/' . $topic->id . '/share.jpg?' . Str::random(10);

            return response()->json(['share_qr' => $res_path]);

        } else {

            $res_path = '/uploads/topic_share_wxmini_qrcode/' . $topic->id . '/share.jpg?' . Str::random(10);

            return response()->json(['share_qr' => $res_path]);
        }

    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章