Goods.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\service\qrcode\bargain;
  3. use Grafika\Color;
  4. use Grafika\Grafika;
  5. use app\common\service\qrcode\Base;
  6. class Goods extends Base
  7. {
  8. // 砍价活动信息
  9. private $active;
  10. // 商品信息
  11. private $goods;
  12. // 用户id
  13. private $user_id;
  14. /**
  15. * 构造方法
  16. * Goods constructor.
  17. * @param $active
  18. * @param $goods
  19. * @param $user
  20. */
  21. public function __construct($active, $goods, $user)
  22. {
  23. parent::__construct();
  24. // 砍价活动信息
  25. $this->active = $active;
  26. // 商品信息
  27. $this->goods = $goods;
  28. // 当前用户id
  29. $this->user_id = $user ? $user['user_id'] : 0;
  30. }
  31. /**
  32. * @return mixed
  33. * @throws \app\common\exception\BaseException
  34. * @throws \think\exception\DbException
  35. * @throws \Exception
  36. */
  37. public function getImage()
  38. {
  39. // 判断海报图文件存在则直接返回url
  40. if (file_exists($this->getPosterPath())) {
  41. return $this->getPosterUrl();
  42. }
  43. // 小程序id
  44. $wxappId = $this->active['wxapp_id'];
  45. // 商品海报背景图
  46. $backdrop = __DIR__ . '/../resource/goods_bg.png';
  47. // 下载商品首图
  48. $goodsUrl = $this->saveTempImage($wxappId, $this->goods['goods_image'], 'goods');
  49. // 小程序码参数
  50. $scene = "aid:{$this->active['active_id']},uid:" . ($this->user_id ?: '');
  51. // 下载小程序码
  52. $qrcode = $this->saveQrcode($wxappId, $scene, 'pages/bargain/goods/index');
  53. // 拼接海报图
  54. return $this->savePoster($backdrop, $goodsUrl, $qrcode);
  55. }
  56. /**
  57. * 拼接海报图
  58. * @param $backdrop
  59. * @param $goodsUrl
  60. * @param $qrcode
  61. * @return string
  62. * @throws \Exception
  63. */
  64. private function savePoster($backdrop, $goodsUrl, $qrcode)
  65. {
  66. // 实例化图像编辑器
  67. $editor = Grafika::createEditor(['Gd']);
  68. // 字体文件路径
  69. $fontPath = Grafika::fontsDir() . '/' . 'st-heiti-light.ttc';
  70. // 打开海报背景图
  71. $editor->open($backdropImage, $backdrop);
  72. // 打开商品图片
  73. $editor->open($goodsImage, $goodsUrl);
  74. // 重设商品图片宽高
  75. $editor->resizeExact($goodsImage, 690, 690);
  76. // 商品图片添加到背景图
  77. $editor->blend($backdropImage, $goodsImage, 'normal', 1.0, 'top-left', 30, 30);
  78. // 商品名称处理换行
  79. $fontSize = 30;
  80. $goodsName = $this->wrapText($fontSize, 0, $fontPath, $this->goods['goods_name'], 680, 2);
  81. // 写入商品名称
  82. $editor->text($backdropImage, $goodsName, $fontSize, 30, 750, new Color('#333333'), $fontPath);
  83. // 写入商品价格
  84. $editor->text($backdropImage, $this->active['floor_price'], 38, 62, 964, new Color('#ff4444'));
  85. // 打开小程序码
  86. $editor->open($qrcodeImage, $qrcode);
  87. // 重设小程序码宽高
  88. $editor->resizeExact($qrcodeImage, 140, 140);
  89. // 小程序码添加到背景图
  90. $editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', 570, 914);
  91. // 保存图片
  92. $editor->save($backdropImage, $this->getPosterPath());
  93. return $this->getPosterUrl();
  94. }
  95. /**
  96. * 处理文字超出长度自动换行
  97. * @param integer $fontsize 字体大小
  98. * @param integer $angle 角度
  99. * @param string $fontface 字体名称
  100. * @param string $string 字符串
  101. * @param integer $width 预设宽度
  102. * @param null $max_line 最多行数
  103. * @return string
  104. */
  105. private function wrapText($fontsize, $angle, $fontface, $string, $width, $max_line = null)
  106. {
  107. // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
  108. $content = "";
  109. // 将字符串拆分成一个个单字 保存到数组 letter 中
  110. $letter = [];
  111. for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
  112. $letter[] = mb_substr($string, $i, 1, 'UTF-8');
  113. }
  114. $line_count = 0;
  115. foreach ($letter as $l) {
  116. $testbox = imagettfbbox($fontsize, $angle, $fontface, $content . ' ' . $l);
  117. // 判断拼接后的字符串是否超过预设的宽度
  118. if (($testbox[2] > $width) && ($content !== "")) {
  119. $line_count++;
  120. if ($max_line && $line_count >= $max_line) {
  121. $content = mb_substr($content, 0, -1, 'UTF-8') . "...";
  122. break;
  123. }
  124. $content .= "\n";
  125. }
  126. $content .= $l;
  127. }
  128. return $content;
  129. }
  130. /**
  131. * 海报图文件路径
  132. * @return string
  133. */
  134. private function getPosterPath()
  135. {
  136. // 保存路径
  137. $tempPath = WEB_PATH . 'temp' . '/' . $this->active['wxapp_id'] . '/';
  138. !is_dir($tempPath) && mkdir($tempPath, 0755, true);
  139. return $tempPath . $this->getPosterName();
  140. }
  141. /**
  142. * 海报图文件名称
  143. * @return string
  144. */
  145. private function getPosterName()
  146. {
  147. return 'bargain_active_' . md5("{$this->user_id}_{$this->active['active_id']}") . '.png';
  148. }
  149. /**
  150. * 海报图url
  151. * @return string
  152. */
  153. private function getPosterUrl()
  154. {
  155. return \base_url() . 'temp/' . $this->active['wxapp_id'] . '/' . $this->getPosterName() . '?t=' . time();
  156. }
  157. }