'imagick']); } $this->main = ImageManagerStatic::make($mainImg); return $this->main->encoded; } /** * 分享帖子生成海报 */ public function post($userInfo, $id) { $postDetail = $this->getPostDetail($id); if(!$postDetail){ return jsonError('获取内容信息失败'); } // 设置头像 if(!$userInfo['avatar']){ $userInfo['avatar'] = 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132'; } if(!$userInfo['username']){ $userInfo['username'] = '我是谁啦啦啦里哦'; } //内容图片 $postImg = ImageManagerStatic::make($postDetail['img'].'!poster_app')->fit(750, 962); $this->main->insert($postImg, 'left-top', 0, 0); //遮罩 $mask = ImageManagerStatic::make(public_path('/image/post/mask.png'))->fit(750, 962); $this->main->insert($mask, 'left-top', 0, 0); //logo $mask = ImageManagerStatic::make(public_path('/image/post/logo.png'))->fit(145, 38); $this->main->insert($mask, 'left-top', 55, 60); //话题 if(isset($postDetail['topic'][0]['name'])){ $this->main->text('#'.$postDetail['topic'][0]['name'].'#', 55, 240, function ($font) { $font->file(public_path('font/PingFang Regular.ttf')); $font->size(26); $font->color('#FFFFFF'); $font->align('left'); }); } //标题或内容 if($postDetail['title']){ $title = $postDetail['title']; }else{ $title = $postDetail['content']; if($postDetail['type'] == 'html'){ $title = strip_tags($postDetail['content']); } } $title = subtext($title, 34); if(mb_strlen($title, 'utf8') > 17){ $title = mb_substr($title, 0, 17, 'utf8')."\n".mb_substr($title, 17, -1, 'utf8').' ...'; $height = 380; }else{ $height = 350; } $this->main->text($title, 55, $height, function ($font) { $font->file(public_path('font/PingFang Medium.ttf')); $font->size(36); $font->color('#FFFFFF'); $font->align('left'); }); // —— $this->main->text('——', 55, 456, function ($font) { $font->file(public_path('font/PingFang Regular.ttf')); $font->size(20); $font->color('#FFFFFF'); $font->align('left'); }); // 用户名称 $this->main->text($userInfo['username'], 155, 470, function ($font) { $font->file(public_path('font/PingFang Regular.ttf')); $font->size(24); $font->color('#FFFFFF'); $font->align('left'); }); //用户头像 $avatar = Image::make($userInfo['avatar'])->resize(42,42); $new= Image::canvas(750, 1334); $r=$avatar->width() /2; for($x=0;$x<$avatar->width();$x++) { for($y=0;$y<$avatar->height();$y++) { $c=$avatar->pickColor($x,$y,'array'); if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) { $new->pixel($c,$x,$y); } } } $this->main->insert($new, 'top-left', 105, 436); $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8')); $qrcode->resize(164,164); $this->main->insert($qrcode,'top-left', 63, 1087); $path = '/public/post'.$id.'.png'; // $image->save($path); //$url = config('filesystems.disks.oss.uri').$path; // 存储图片 // $url = Storage::put($path, $image->basePath()); return $this->main->response('png'); } }