PosterRepository.php 4.4 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 Intervention\Image\Facades\Image;
  11. use Intervention\Image\ImageManagerStatic;
  12. use Illuminate\Support\Facades\Storage;
  13. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  14. class PosterRepository
  15. {
  16. use CommunityTrait;
  17. public $main;
  18. public function __construct($mainImg)
  19. {
  20. if (extension_loaded('imagick')) {
  21. ImageManagerStatic::configure(['driver'=>'imagick']);
  22. }
  23. // $this->main = ImageManagerStatic::make($mainImg);
  24. // return $this->main->encoded;
  25. }
  26. /**
  27. * 分享帖子生成海报
  28. */
  29. public function post($mainImg,$userInfo, $id)
  30. {
  31. $main = ImageManagerStatic::make($mainImg);
  32. $postDetail = $this->getPostDetail($id);
  33. if(!$postDetail){
  34. return jsonError('获取内容信息失败');
  35. }
  36. // 设置头像
  37. if(!$userInfo['avatar']){
  38. $userInfo['avatar'] = 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132';
  39. }
  40. if(!$userInfo['username']){
  41. $userInfo['username'] = '我是谁啦啦啦里哦';
  42. }
  43. //内容图片
  44. $postImg = ImageManagerStatic::make($postDetail['img'].'!poster_app')->fit(750, 962);
  45. $main->insert($postImg, 'left-top', 0, 0);
  46. //遮罩
  47. $mask = ImageManagerStatic::make(public_path('/image/post/mask.png'))->fit(750, 962);
  48. $main->insert($mask, 'left-top', 0, 0);
  49. //logo
  50. $mask = ImageManagerStatic::make(public_path('/image/post/logo.png'))->fit(145, 38);
  51. $main->insert($mask, 'left-top', 55, 60);
  52. //话题
  53. if(isset($postDetail['topic'][0]['name'])){
  54. $main->text('#'.$postDetail['topic'][0]['name'].'#', 55, 240, function ($font) {
  55. $font->file(public_path('font/PingFang Regular.ttf'));
  56. $font->size(26);
  57. $font->color('#FFFFFF');
  58. $font->align('left');
  59. });
  60. }
  61. //标题或内容
  62. if($postDetail['title']){
  63. $title = $postDetail['title'];
  64. }else{
  65. $title = $postDetail['content'];
  66. if($postDetail['type'] == 'html'){
  67. $title = strip_tags($postDetail['content']);
  68. }
  69. }
  70. $title = subtext($title, 34);
  71. if(mb_strlen($title, 'utf8') > 17){
  72. $title = mb_substr($title, 0, 17, 'utf8')."\n".mb_substr($title, 17, -1, 'utf8').' ...';
  73. $height = 380;
  74. }else{
  75. $height = 350;
  76. }
  77. $main->text($title, 55, $height, function ($font) {
  78. $font->file(public_path('font/PingFang Medium.ttf'));
  79. $font->size(36);
  80. $font->color('#FFFFFF');
  81. $font->align('left');
  82. });
  83. // ——
  84. $main->text('——', 55, 456, function ($font) {
  85. $font->file(public_path('font/PingFang Regular.ttf'));
  86. $font->size(20);
  87. $font->color('#FFFFFF');
  88. $font->align('left');
  89. });
  90. // 用户名称
  91. $main->text($userInfo['username'], 155, 470, function ($font) {
  92. $font->file(public_path('font/PingFang Regular.ttf'));
  93. $font->size(24);
  94. $font->color('#FFFFFF');
  95. $font->align('left');
  96. });
  97. //用户头像
  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. $qrcode = Image::make(QrCode::format('png')->generate('https://www.jianshu.com/p/1c78294f26f8'));
  111. $qrcode->resize(164,164);
  112. $main->insert($qrcode,'top-left', 63, 1087);
  113. // $main->save(public_path('test/post_id'.$id.'.png'));
  114. // return $response = $main->response();
  115. // 存储图片
  116. $filename = date('Ym').'/'.time() . '_' . uniqid() . '.jpg';
  117. $url = Storage::put($filename, (string)$main->encode('jpg'));
  118. return $filename;
  119. }
  120. }