laravel后端服务器生成微信小程序海报api示例代码
后端用laravel框架实现功能接口 + 前端你用啥都可以,小程序可以用uniapp/原生,其他的只要能接收json就ok。
代码示例:
<?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]);
}
}
}
ps:
我看到也有很多把类似功能做成包的方式,嫌麻烦就可以用上面这种方式;
相关文章