PosterRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $bean = 500;
  37. $beanWidth = 540;
  38. if(strlen($bean) == 4) $beanWidth = 532;
  39. $main->text($bean, $beanWidth, 1214, function ($font) {
  40. $font->file(public_path('font/PingFang Bold.ttf'));
  41. $font->size(28);
  42. $font->color('#FFFFFF');
  43. $font->align('left');
  44. });
  45. //内容图片
  46. $postImg = ImageManagerStatic::make($postDetail['img'].'!poster_app')->fit(750, 962);
  47. $main->insert($postImg, 'left-top', 0, 0);
  48. //遮罩
  49. $mask = ImageManagerStatic::make(public_path('/image/post/mask.png'))->fit(750, 962);
  50. $main->insert($mask, 'left-top', 0, 0);
  51. //logo
  52. $mask = ImageManagerStatic::make(public_path('/image/post/logo.png'))->fit(145, 38);
  53. $main->insert($mask, 'left-top', 55, 60);
  54. //话题
  55. if(isset($postDetail['topic'][0]['name'])){
  56. $main->text('#'.$postDetail['topic'][0]['name'].'#', 55, 240, function ($font) {
  57. $font->file(public_path('font/PingFang Regular.ttf'));
  58. $font->size(26);
  59. $font->color('#FFFFFF');
  60. $font->align('left');
  61. });
  62. }
  63. //标题或内容
  64. if($postDetail['title']){
  65. $title = $postDetail['title'];
  66. }else{
  67. $title = $postDetail['content'];
  68. if($postDetail['type'] == 'html'){
  69. $title = strip_tags($postDetail['content']);
  70. }
  71. }
  72. $title = subtext($title, 34);
  73. if(mb_strlen($title, 'utf8') > 17){
  74. $title = mb_substr($title, 0, 17, 'utf8')."\n".mb_substr($title, 17, -1, 'utf8').' ...';
  75. $height = 380;
  76. }else{
  77. $height = 350;
  78. }
  79. $main->text($title, 55, $height, function ($font) {
  80. $font->file(public_path('font/PingFang Medium.ttf'));
  81. $font->size(36);
  82. $font->color('#FFFFFF');
  83. $font->align('left');
  84. });
  85. // ——
  86. $main->text('——', 55, 456, function ($font) {
  87. $font->file(public_path('font/PingFang Regular.ttf'));
  88. $font->size(20);
  89. $font->color('#FFFFFF');
  90. $font->align('left');
  91. });
  92. // 用户名称
  93. if($userInfo['username']){
  94. $main->text($userInfo['username'], 155, 470, function ($font) {
  95. $font->file(public_path('font/PingFang Regular.ttf'));
  96. $font->size(24);
  97. $font->color('#FFFFFF');
  98. $font->align('left');
  99. });
  100. }
  101. // 用户头像
  102. if($userInfo['avatar']){
  103. $avatar = Image::make($userInfo['avatar'])->resize(42,42);
  104. $new= Image::canvas(750, 1334);
  105. $r=$avatar->width() /2;
  106. for($x=0;$x<$avatar->width();$x++) {
  107. for($y=0;$y<$avatar->height();$y++) {
  108. $c=$avatar->pickColor($x,$y,'array');
  109. if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
  110. $new->pixel($c,$x,$y);
  111. }
  112. }
  113. }
  114. $main->insert($new, 'top-left', 105, 436);
  115. }
  116. $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8'));
  117. $qrcode->resize(164,164);
  118. $main->insert($qrcode,'top-left', 63, 1087);
  119. // 存储图片
  120. $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
  121. Storage::put($filename, (string)$main->encode('jpg'));
  122. $url = 'http://oss.caihongxingqiu.net/'.$filename;
  123. Redis::set($key, $url);
  124. Redis::expire($key, 3600 * 24 * 3);
  125. return jsonSuccess($url);
  126. }
  127. /**
  128. * 邀请海报
  129. */
  130. public function invite($mainImg,$userInfo,$key, $bean)
  131. {
  132. $main = ImageManagerStatic::make($mainImg);
  133. // 彩虹豆数
  134. $beanWidth = 194;
  135. if(strlen($bean) == 4) $beanWidth = 182;
  136. $main->text($bean, $beanWidth, 1060, function ($font) {
  137. $font->file(public_path('font/PingFang Bold.ttf'));
  138. $font->size(36);
  139. $font->color('#C74A48');
  140. $font->align('left');
  141. });
  142. // 邀请码
  143. if($userInfo['invite_code']){
  144. $str = $userInfo['invite_code'];
  145. $strLower = strtolower($str);
  146. $upperNumber = 0;
  147. for($i=0;$i<6;$i++){
  148. if($str[$i] != $strLower[$i]) $upperNumber++;
  149. }
  150. $codeWidth = 212 - $upperNumber * 3;
  151. $main->text($str, $codeWidth, 912, function ($font) {
  152. $font->file(public_path('font/PingFang Regular.ttf'));
  153. $font->size(28);
  154. $font->color('#3C5852');
  155. $font->align('left');
  156. });
  157. }
  158. // 用户名称
  159. if($userInfo['username']){
  160. $main->text($userInfo['username'], 30, 860, function ($font) {
  161. $font->file(public_path('font/PingFang Regular.ttf'));
  162. $font->size(28);
  163. $font->color('#3C5852');
  164. $font->align('left');
  165. });
  166. }
  167. // 用户头像
  168. if($userInfo['avatar']){
  169. $avatar = Image::make($userInfo['avatar'])->resize(88,88);
  170. $new= Image::canvas(750, 1346);
  171. $r=$avatar->width() /2;
  172. for($x=0;$x<$avatar->width();$x++) {
  173. for($y=0;$y<$avatar->height();$y++) {
  174. $c=$avatar->pickColor($x,$y,'array');
  175. if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
  176. $new->pixel($c,$x,$y);
  177. }
  178. }
  179. }
  180. $main->insert($new, 'top-left', 30, 717);
  181. }
  182. $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8'));
  183. $qrcode->resize(170,170);
  184. $main->insert($qrcode,'top-left', 547, 944);
  185. // 存储图片
  186. $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
  187. Storage::put($filename, (string)$main->encode('jpg'));
  188. $url = 'http://oss.caihongxingqiu.net/'.$filename;
  189. Redis::set($key, $url);
  190. Redis::expire($key, 3600 * 24 * 3);
  191. return jsonSuccess($url);
  192. }
  193. }