PosterRepository.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /**
  123. * 邀请海报
  124. */
  125. public function invite($mainImg,$userInfo,$key, $bean)
  126. {
  127. $main = ImageManagerStatic::make($mainImg);
  128. if(!$userInfo['username']){
  129. $userInfo['username'] = '';
  130. }
  131. // 彩虹豆数
  132. $beanWidth = 194;
  133. if(strlen($bean) == 4) $beanWidth = 182;
  134. $main->text($bean, $beanWidth, 1060, function ($font) {
  135. $font->file(public_path('font/PingFang Bold.ttf'));
  136. $font->size(36);
  137. $font->color('#C74A48');
  138. $font->align('left');
  139. });
  140. // 邀请码
  141. if($userInfo['invite_code']){
  142. $str = $userInfo['invite_code'];
  143. $strLower = strtolower($str);
  144. $upperNumber = 0;
  145. for($i=0;$i<6;$i++){
  146. if($str[$i] != $strLower[$i]) $upperNumber++;
  147. }
  148. $codeWidth = 212 - $upperNumber * 3;
  149. $main->text($str, $codeWidth, 912, function ($font) {
  150. $font->file(public_path('font/PingFang Regular.ttf'));
  151. $font->size(28);
  152. $font->color('#3C5852');
  153. $font->align('left');
  154. });
  155. }
  156. // 用户名称
  157. $main->text($userInfo['username'], 30, 860, function ($font) {
  158. $font->file(public_path('font/PingFang Regular.ttf'));
  159. $font->size(28);
  160. $font->color('#3C5852');
  161. $font->align('left');
  162. });
  163. // 用户头像
  164. if($userInfo['avatar']){
  165. $avatar = Image::make($userInfo['avatar'])->resize(88,88);
  166. $new= Image::canvas(750, 1346);
  167. $r=$avatar->width() /2;
  168. for($x=0;$x<$avatar->width();$x++) {
  169. for($y=0;$y<$avatar->height();$y++) {
  170. $c=$avatar->pickColor($x,$y,'array');
  171. if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
  172. $new->pixel($c,$x,$y);
  173. }
  174. }
  175. }
  176. $main->insert($new, 'top-left', 30, 717);
  177. }
  178. $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8'));
  179. $qrcode->resize(170,170);
  180. $main->insert($qrcode,'top-left', 547, 944);
  181. // 存储图片
  182. $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
  183. Storage::put($filename, (string)$main->encode('jpg'));
  184. $url = 'http://oss.caihongxingqiu.net/'.$filename;
  185. Redis::set($key, $url);
  186. Redis::expire($key, 3600 * 24 * 3);
  187. return jsonSuccess($url);
  188. }
  189. }