PosterRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/20
  6. * Time: 16:50
  7. */
  8. namespace App\Repositories;
  9. use App\Traits\CommunityTrait;
  10. use Illuminate\Support\Facades\Redis;
  11. use Intervention\Image\Facades\Image;
  12. use Intervention\Image\ImageManagerStatic;
  13. use Illuminate\Support\Facades\Storage;
  14. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  15. class PosterRepository
  16. {
  17. use CommunityTrait;
  18. public $main;
  19. public function __construct($mainImg)
  20. {
  21. if (extension_loaded('imagick')) {
  22. ImageManagerStatic::configure(['driver'=>'imagick']);
  23. }
  24. }
  25. /**
  26. * 分享帖子生成海报
  27. */
  28. public function post($mainImg,$userInfo,$id,$key)
  29. {
  30. $main = ImageManagerStatic::make($mainImg);
  31. $postDetail = $this->getPostDetail($id);
  32. if(!$postDetail){
  33. return jsonError('获取内容信息失败');
  34. }
  35. // 设置头像
  36. if(!$userInfo['avatar']){
  37. $userInfo['avatar'] = 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132';
  38. }
  39. if(!$userInfo['username']){
  40. $userInfo['username'] = '';
  41. }
  42. //内容图片
  43. $postImg = ImageManagerStatic::make($postDetail['img'].'!poster_app')->fit(750, 962);
  44. $main->insert($postImg, 'left-top', 0, 0);
  45. //遮罩
  46. $mask = ImageManagerStatic::make(public_path('/image/post/mask.png'))->fit(750, 962);
  47. $main->insert($mask, 'left-top', 0, 0);
  48. //logo
  49. $mask = ImageManagerStatic::make(public_path('/image/post/logo.png'))->fit(145, 38);
  50. $main->insert($mask, 'left-top', 55, 60);
  51. //话题
  52. if(isset($postDetail['topic'][0]['name'])){
  53. $main->text('#'.$postDetail['topic'][0]['name'].'#', 55, 240, function ($font) {
  54. $font->file(public_path('font/PingFang Regular.ttf'));
  55. $font->size(26);
  56. $font->color('#FFFFFF');
  57. $font->align('left');
  58. });
  59. }
  60. //标题或内容
  61. if($postDetail['title']){
  62. $title = $postDetail['title'];
  63. }else{
  64. $title = $postDetail['content'];
  65. if($postDetail['type'] == 'html'){
  66. $title = strip_tags($postDetail['content']);
  67. }
  68. }
  69. $title = subtext($title, 34);
  70. if(mb_strlen($title, 'utf8') > 17){
  71. $title = mb_substr($title, 0, 17, 'utf8')."\n".mb_substr($title, 17, -1, 'utf8').' ...';
  72. $height = 380;
  73. }else{
  74. $height = 350;
  75. }
  76. $main->text($title, 55, $height, function ($font) {
  77. $font->file(public_path('font/PingFang Medium.ttf'));
  78. $font->size(36);
  79. $font->color('#FFFFFF');
  80. $font->align('left');
  81. });
  82. // ——
  83. $main->text('——', 55, 456, function ($font) {
  84. $font->file(public_path('font/PingFang Regular.ttf'));
  85. $font->size(20);
  86. $font->color('#FFFFFF');
  87. $font->align('left');
  88. });
  89. // 用户名称
  90. $main->text($userInfo['username'], 155, 470, function ($font) {
  91. $font->file(public_path('font/PingFang Regular.ttf'));
  92. $font->size(24);
  93. $font->color('#FFFFFF');
  94. $font->align('left');
  95. });
  96. // 用户头像
  97. if($userInfo['avatar']){
  98. $avatar = Image::make($userInfo['avatar'])->resize(42,42);
  99. $new= Image::canvas(750, 1334);
  100. $r=$avatar->width() /2;
  101. for($x=0;$x<$avatar->width();$x++) {
  102. for($y=0;$y<$avatar->height();$y++) {
  103. $c=$avatar->pickColor($x,$y,'array');
  104. if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
  105. $new->pixel($c,$x,$y);
  106. }
  107. }
  108. }
  109. $main->insert($new, 'top-left', 105, 436);
  110. }
  111. $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8'));
  112. $qrcode->resize(164,164);
  113. $main->insert($qrcode,'top-left', 63, 1087);
  114. // 存储图片
  115. $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
  116. Storage::put($filename, (string)$main->encode('jpg'));
  117. $url = 'http://oss.caihongxingqiu.net/'.$filename;
  118. Redis::set($key, $url);
  119. Redis::expire($key, 3600 * 24 * 3);
  120. return jsonSuccess($url);
  121. }
  122. }