123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/20
- * Time: 16:50
- */
- namespace App\Repositories;
- use App\Traits\CommunityTrait;
- use Illuminate\Support\Facades\Redis;
- use Intervention\Image\Facades\Image;
- use Intervention\Image\ImageManagerStatic;
- use Illuminate\Support\Facades\Storage;
- use SimpleSoftwareIO\QrCode\Facades\QrCode;
- class PosterRepository
- {
- use CommunityTrait;
- public $main;
- public function __construct($mainImg)
- {
- if (extension_loaded('imagick')) {
- ImageManagerStatic::configure(['driver'=>'imagick']);
- }
- }
- /**
- * 分享内容海报
- */
- public function post($mainImg,$userInfo,$id,$key)
- {
- $main = ImageManagerStatic::make($mainImg);
- $postDetail = $this->getPostDetail($id);
- if(!$postDetail){
- return jsonError('获取内容信息失败');
- }
- // U米数
- $bean = config('customer.share_post_bean');
- $beanWidth = 540;
- if(strlen($bean) == 4) $beanWidth = 532;
- $main->text($bean, $beanWidth, 1214, function ($font) {
- $font->file(public_path('font/PingFang Bold.ttf'));
- $font->size(28);
- $font->color('#FFFFFF');
- $font->align('left');
- });
- //内容图片
- $postImg = ImageManagerStatic::make($postDetail['img'].'!poster_app')->fit(750, 962);
- $main->insert($postImg, 'left-top', 0, 0);
- //遮罩
- $mask = ImageManagerStatic::make(public_path('/image/post/mask.png'))->fit(750, 962);
- $main->insert($mask, 'left-top', 0, 0);
- //logo
- $mask = ImageManagerStatic::make(public_path('/image/post/logo.png'))->fit(145, 38);
- $main->insert($mask, 'left-top', 55, 60);
- //话题
- if(isset($postDetail['topic'][0]['name'])){
- $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;
- }
- $main->text($title, 55, $height, function ($font) {
- $font->file(public_path('font/PingFang Medium.ttf'));
- $font->size(36);
- $font->color('#FFFFFF');
- $font->align('left');
- });
- // ——
- $main->text('——', 55, 456, function ($font) {
- $font->file(public_path('font/PingFang Regular.ttf'));
- $font->size(20);
- $font->color('#FFFFFF');
- $font->align('left');
- });
- // 用户名称
- if($userInfo['username']){
- $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');
- });
- }
- // 用户头像
- if($userInfo['avatar']){
- $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);
- }
- }
- }
- $main->insert($new, 'top-left', 105, 436);
- }
- $qrcode = Image::make(QrCode::format('png')->generate(config('customer.share_post_h5url')."?post_id={$id}&invite_code={$userInfo['invite_code']}"));
- $qrcode->resize(164,164);
- $main->insert($qrcode,'top-left', 63, 1087);
- // 存储图片
- $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
- Storage::put($filename, (string)$main->encode('jpg'));
- $url = 'http://oss.caihongxingqiu.net/'.$filename;
- Redis::set($key, $url);
- Redis::expire($key, 3600 * 24 * 3);
- return jsonSuccess($url);
- }
- /**
- * 邀请海报
- */
- public function invite($mainImg,$userInfo,$key, $bean)
- {
- $main = ImageManagerStatic::make($mainImg);
- // U米数
- $beanWidth = 180;
- if(strlen($bean) == 4) $beanWidth = 168;
- $main->text($bean, $beanWidth, 1014, function ($font) {
- $font->file(public_path('font/PingFang Bold.ttf'));
- $font->size(34);
- $font->color('#0CD4C0');
- $font->align('left');
- });
- // 邀请码
- if($userInfo['invite_code']){
- $str = $userInfo['invite_code'];
- $strLower = strtolower($str);
- $upperNumber = 0;
- for($i=0;$i<6;$i++){
- if($str[$i] != $strLower[$i]) $upperNumber++;
- }
- $codeWidth = 592 - $upperNumber * 3;
- $main->text($str, $codeWidth, 892, function ($font) {
- $font->file(public_path('font/PingFang Bold.ttf'));
- $font->size(32);
- $font->color('#0CD4C0');
- $font->align('left');
- });
- }
- // 用户名称
- if($userInfo['username']){
- $userName = filter_Emoji($userInfo['username']);
- $userName = subtext($userName, 9);
- $main->text($userName, 40, 892, function ($font) {
- $font->file(public_path('font/PingFang Bold.ttf'));
- $font->size(32);
- $font->color('#333333');
- $font->align('left');
- });
- }
- // 用户头像
- if($userInfo['avatar']){
- $avatar = Image::make($userInfo['avatar'])->resize(100,100);
- $new= Image::canvas(750, 1346);
- $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);
- }
- }
- }
- $main->insert($new, 'top-left', 40, 744);
- }
- $qrcode = Image::make(QrCode::format('png')->generate(config('customer.share_invite_h5url')."?invite_code={$userInfo['invite_code']}"));
- $qrcode->resize(140,140);
- $main->insert($qrcode,'top-left', 570, 1164);
- // 存储图片
- $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
- Storage::put($filename, (string)$main->encode('jpg'));
- $url = 'http://oss.caihongxingqiu.net/'.$filename;
- Redis::set($key, $url);
- Redis::expire($key, 3600 * 24 * 3);
- return jsonSuccess($url);
- }
- }
|