PosterService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\common\service\qrcode;
  3. use app\common\model\settings\Setting as SettingModel;
  4. use Endroid\QrCode\QrCode;
  5. use Grafika\Color;
  6. use Grafika\Grafika;
  7. use app\common\model\plus\agent\Setting;
  8. /**
  9. * 分销二维码
  10. */
  11. class PosterService extends Base
  12. {
  13. // 分销商用户信息
  14. private $agent;
  15. // 分销商海报设置
  16. private $config;
  17. // 来源
  18. private $source;
  19. /**
  20. * 构造方法
  21. */
  22. public function __construct($agent, $source)
  23. {
  24. parent::__construct();
  25. // 分销商用户信息
  26. $this->agent = $agent;
  27. $this->source = $source;
  28. // 分销商海报设置
  29. $this->config = Setting::getItem('qrcode', $agent['app_id']);
  30. }
  31. /**
  32. * 获取分销二维码
  33. */
  34. public function getImage()
  35. {
  36. if (file_exists($this->getPosterPath())) {
  37. return $this->getPosterUrl();
  38. }
  39. // 小程序id
  40. $appId = $this->agent['app_id'];
  41. // 1. 下载背景图
  42. $backdrop = $this->saveTempImage($appId, $this->config['backdrop']['src'], 'backdrop');
  43. // 2. 下载用户头像
  44. $avatarUrl = $this->saveTempImage($appId, $this->agent['user']['avatarUrl'], 'avatar');
  45. if($this->source == 'wx') {
  46. // 3. 下载小程序码
  47. $scene = 'uid:' . $this->agent['user_id'];
  48. $qrcode = $this->saveQrcode($appId, $scene, 'pages/index/index');
  49. } else if($this->source == 'mp' || $this->source == 'h5'){
  50. $scene = 'uid:' . $this->agent['user_id'];
  51. $qrcode = new QrCode(base_url().'h5/pages/index/index?referee_id='.$this->agent['user_id'].'&app_id='.$appId);
  52. $qrcode = $this->saveMpQrcode($qrcode, $appId, $scene, 'mp');
  53. } else if($this->source == 'app'){
  54. $appshare = SettingModel::getItem('appshare');
  55. if($appshare['type'] == 1){
  56. $down_url = $appshare['open_site']. '?app_id='.$this->agent['app_id'].'&referee_id=' .$this->agent['user_id'];
  57. }else{
  58. //下载页
  59. if($appshare['bind_type'] == 1){
  60. $down_url = $appshare['down_url'];
  61. }else{
  62. $down_url = base_url(). "/index.php/api/user.useropen/invite?app_id=".$this->agent['app_id']."&referee_id=" .$this->agent['user_id'];
  63. }
  64. }
  65. $scene = 'uid:' . $this->agent['user_id'];
  66. $qrcode = new QrCode($down_url);
  67. $qrcode = $this->saveMpQrcode($qrcode, $appId, $scene, 'mp');
  68. }
  69. // 4. 拼接海报图
  70. return $this->savePoster($backdrop, $avatarUrl, $qrcode);
  71. }
  72. /**
  73. * 海报图文件路径
  74. */
  75. private function getPosterPath()
  76. {
  77. // 保存路径
  78. $tempPath = root_path('public') . 'temp/' . $this->agent['app_id'] . '/' . $this->source. '/';
  79. !is_dir($tempPath) && mkdir($tempPath, 0755, true);
  80. return $tempPath . $this->getPosterName();
  81. }
  82. /**
  83. * 海报图文件名称
  84. */
  85. private function getPosterName()
  86. {
  87. return 'poster_' . md5($this->agent['user_id']) . '.png';
  88. }
  89. /**
  90. * 海报图url
  91. */
  92. private function getPosterUrl()
  93. {
  94. return \base_url() . 'temp/' . $this->agent['app_id'] . '/' .$this->source . '/' . $this->getPosterName() . '?t=' . time();
  95. }
  96. /**
  97. * 拼接海报图
  98. */
  99. private function savePoster($backdrop, $avatarUrl, $qrcode)
  100. {
  101. // 实例化图像编辑器
  102. $editor = Grafika::createEditor(['Gd']);
  103. // 打开海报背景图
  104. $editor->open($backdropImage, $backdrop);
  105. // 生成圆形用户头像
  106. $this->config['avatar']['style'] === 'circle' && $this->circular($avatarUrl, $avatarUrl);
  107. // 打开用户头像
  108. $editor->open($avatarImage, $avatarUrl);
  109. // 重设用户头像宽高
  110. $avatarWidth = $this->config['avatar']['width'] * 2;
  111. $editor->resizeExact($avatarImage, $avatarWidth, $avatarWidth);
  112. // 用户头像添加到背景图
  113. $avatarX = $this->config['avatar']['left'] * 2;
  114. $avatarY = $this->config['avatar']['top'] * 2;
  115. $editor->blend($backdropImage, $avatarImage, 'normal', 1.0, 'top-left', $avatarX, $avatarY);
  116. // 生成圆形小程序码,仅小程序支持
  117. if($this->source == 'wx'){
  118. $this->config['qrcode']['style'] === 'circle' && $this->circular($qrcode, $qrcode);
  119. }
  120. // 打开小程序码
  121. $editor->open($qrcodeImage, $qrcode);
  122. // 重设小程序码宽高
  123. $qrcodeWidth = $this->config['qrcode']['width'] * 2;
  124. $editor->resizeExact($qrcodeImage, $qrcodeWidth, $qrcodeWidth);
  125. // 小程序码添加到背景图
  126. $qrcodeX = $this->config['qrcode']['left'] * 2;
  127. $qrcodeY = $this->config['qrcode']['top'] * 2;
  128. $editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', $qrcodeX, $qrcodeY);
  129. // 写入用户昵称
  130. $fontSize = $this->config['nickName']['fontSize'] * 2;
  131. $fontX = $this->config['nickName']['left'] * 2;
  132. $fontY = $this->config['nickName']['top'] * 2;
  133. $Color = new Color($this->config['nickName']['color']);
  134. $fontPath = Grafika::fontsDir() . '/' . 'st-heiti-light.ttc';
  135. $editor->text($backdropImage, $this->agent['user']['nickName'], $fontSize, $fontX, $fontY, $Color, $fontPath);
  136. // 保存图片
  137. $editor->save($backdropImage, $this->getPosterPath());
  138. return $this->getPosterUrl();
  139. }
  140. /**
  141. * 生成圆形图片
  142. */
  143. private function circular($imgpath, $saveName = '')
  144. {
  145. $srcImg = imagecreatefromstring(file_get_contents($imgpath));
  146. $w = imagesx($srcImg);
  147. $h = imagesy($srcImg);
  148. $w = $h = min($w, $h);
  149. $newImg = imagecreatetruecolor($w, $h);
  150. // 这一句一定要有
  151. imagesavealpha($newImg, true);
  152. // 拾取一个完全透明的颜色,最后一个参数127为全透明
  153. $bg = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  154. imagefill($newImg, 0, 0, $bg);
  155. $r = $w / 2; //圆半径
  156. for ($x = 0; $x < $w; $x++) {
  157. for ($y = 0; $y < $h; $y++) {
  158. $rgbColor = imagecolorat($srcImg, $x, $y);
  159. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  160. imagesetpixel($newImg, $x, $y, $rgbColor);
  161. }
  162. }
  163. }
  164. // 输出图片到文件
  165. imagepng($newImg, $saveName);
  166. // 释放空间
  167. imagedestroy($srcImg);
  168. imagedestroy($newImg);
  169. }
  170. }