Goods.php 5.8 KB

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