PosterUtil.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace common\components;
  3. /**
  4. * 散客分享海报(OSS:poster/blank.png 动态画布 + 水印合成)
  5. */
  6. class PosterUtil
  7. {
  8. /** 与 poster/1234.jpg 同目录的 10×10 白底,经 m_pad 拉成本次海报画布 */
  9. const BLANK_CANVAS = 'poster/blank.png';
  10. const FONT = 'd3F5LW1pY3JvaGVp';
  11. const POSTER_W = 750;
  12. /** 画布左右、顶部留白(各 20px) */
  13. const CANVAS_PAD_X = 20;
  14. const CANVAS_PAD_Y = 20;
  15. /** 底部文案每行最多字数 */
  16. const NAME_CHARS_PER_LINE = 13;
  17. /** 商品图显示宽度 = POSTER_W - 2×CANVAS_PAD_X */
  18. const SLOT_W = 710;
  19. /** 底部价格/标题/太阳码固定区 */
  20. const FOOTER_H = 360;
  21. /** 主图区温和下限,避免扁图海报过矮 */
  22. const IMG_H_MIN = 240;
  23. const IMG_H_MAX = 880;
  24. const TOTAL_H_MAX = 1600;
  25. const CANVAS_COLOR = 'FFFFFF';
  26. const QR_SIZE = 220;
  27. const QR_PAD_RIGHT = 36;
  28. const QR_PAD_BOTTOM = 32;
  29. const QR_HINT_GAP = 14;
  30. const SCAN_HINT = '微信扫码下单';
  31. const FOOTER_STAFF_Y = 32;
  32. const FOOTER_TEXT_GAP = 12;
  33. const FOOTER_PRICE_SIZE = 56;
  34. const FOOTER_NAME_SIZE = 36;
  35. const FOOTER_STAFF_SIZE = 26;
  36. /**
  37. * 海报标题过长时截断,避免压住底部文案区
  38. */
  39. public static function truncateText($text, $maxLen = 14)
  40. {
  41. $text = trim((string)$text);
  42. if ($text === '') {
  43. return '';
  44. }
  45. if (mb_strlen($text, 'UTF-8') > $maxLen) {
  46. return mb_substr($text, 0, $maxLen, 'UTF-8') . '…';
  47. }
  48. return $text;
  49. }
  50. /**
  51. * 商品名拆成最多两行:首行 13 字;次行剩余,超过 13 字则省略
  52. *
  53. * @return array{0:string,1:string}
  54. */
  55. public static function splitPosterNameLines($name, $maxPerLine = self::NAME_CHARS_PER_LINE)
  56. {
  57. $name = trim((string)$name);
  58. if ($name === '') {
  59. return ['', ''];
  60. }
  61. $maxPerLine = (int)$maxPerLine;
  62. if ($maxPerLine < 1) {
  63. $maxPerLine = self::NAME_CHARS_PER_LINE;
  64. }
  65. $len = mb_strlen($name, 'UTF-8');
  66. if ($len <= $maxPerLine) {
  67. return [$name, ''];
  68. }
  69. $line1 = mb_substr($name, 0, $maxPerLine, 'UTF-8');
  70. $rest = mb_substr($name, $maxPerLine, null, 'UTF-8');
  71. $restLen = mb_strlen($rest, 'UTF-8');
  72. if ($restLen <= $maxPerLine) {
  73. return [$line1, $rest];
  74. }
  75. $line2 = mb_substr($rest, 0, $maxPerLine, 'UTF-8') . '…';
  76. return [$line1, $line2];
  77. }
  78. /**
  79. * 商品名水印文案(支持 \\n 两行),支数/副标题尽量接在末行
  80. */
  81. public static function buildPosterNameText($name, $flowerNum = 0, $subtitle = '')
  82. {
  83. list($line1, $line2) = self::splitPosterNameLines($name);
  84. $suffix = '';
  85. if ($flowerNum > 0) {
  86. $suffix = ' · ' . (int)$flowerNum . '支';
  87. } elseif (trim((string)$subtitle) !== '') {
  88. $suffix = ' · ' . trim((string)$subtitle);
  89. }
  90. if ($suffix !== '') {
  91. $target = $line2 !== '' ? 'line2' : 'line1';
  92. if ($target === 'line1') {
  93. $merged = $line1 . $suffix;
  94. if (mb_strlen($merged, 'UTF-8') <= self::NAME_CHARS_PER_LINE) {
  95. $line1 = $merged;
  96. } else {
  97. $line2 = self::truncateText($suffix, self::NAME_CHARS_PER_LINE);
  98. }
  99. } else {
  100. $merged = $line2 . $suffix;
  101. if (mb_strlen($merged, 'UTF-8') <= self::NAME_CHARS_PER_LINE) {
  102. $line2 = $merged;
  103. } else {
  104. $line2 = self::truncateText($merged, self::NAME_CHARS_PER_LINE);
  105. }
  106. }
  107. }
  108. if ($line2 === '') {
  109. return $line1;
  110. }
  111. return $line1 . "\n" . $line2;
  112. }
  113. /**
  114. * 底部文案 y(g_sw,自下而上:推荐语 → 价格 → 商品名)
  115. */
  116. private static function footerTextYPositions($nameLineCount)
  117. {
  118. $staffY = self::FOOTER_STAFF_Y;
  119. $staffH = (int)ceil(self::FOOTER_STAFF_SIZE * 1.3);
  120. $priceY = $staffY + $staffH + self::FOOTER_TEXT_GAP;
  121. $priceH = (int)ceil(self::FOOTER_PRICE_SIZE * 1.1);
  122. $nameLineCount = max(1, min(2, (int)$nameLineCount));
  123. $nameLineH = (int)ceil(self::FOOTER_NAME_SIZE * 1.15);
  124. $nameY = $priceY + $priceH + self::FOOTER_TEXT_GAP + ($nameLineCount - 1) * $nameLineH;
  125. return [
  126. 'staff' => $staffY,
  127. 'price' => $priceY,
  128. 'name' => $nameY,
  129. ];
  130. }
  131. /**
  132. * 按商品图比例计算主图区高度与画布总高(能多矮就多矮,仅保留温和下限)
  133. *
  134. * @return array{imgH:int,totalH:int,scaledH:int,posterW:int}
  135. */
  136. public static function calcPosterLayout($coverWidth, $coverHeight)
  137. {
  138. $coverWidth = (int)$coverWidth;
  139. $coverHeight = (int)$coverHeight;
  140. $scaledH = 0;
  141. if ($coverWidth > 0 && $coverHeight > 0) {
  142. $scaledH = (int)round($coverHeight * (self::SLOT_W / $coverWidth));
  143. $imgH = max(self::IMG_H_MIN, min(self::IMG_H_MAX, $scaledH));
  144. } else {
  145. $scaledH = self::IMG_H_MIN;
  146. $imgH = self::IMG_H_MIN;
  147. }
  148. $totalH = $imgH + self::FOOTER_H;
  149. $absoluteMin = self::FOOTER_H + 120;
  150. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, $totalH));
  151. return [
  152. 'imgH' => $imgH,
  153. 'totalH' => $totalH,
  154. 'scaledH' => $scaledH,
  155. 'posterW' => self::POSTER_W,
  156. ];
  157. }
  158. /**
  159. * 商品图:常规按宽 SLOT_W 等比(无左右垫白);超高图用 m_fill 裁高至 IMG_H_MAX
  160. */
  161. private static function buildCoverWatermarkPath($cover, $scaledH)
  162. {
  163. $scaledH = (int)$scaledH;
  164. if ($scaledH > self::IMG_H_MAX) {
  165. return $cover . '?x-oss-process=image/resize,m_fill,w_' . self::SLOT_W . ',h_' . self::IMG_H_MAX . ',limit_0';
  166. }
  167. return $cover . '?x-oss-process=image/resize,m_lfit,w_' . self::SLOT_W . ',limit_0';
  168. }
  169. /**
  170. * 太阳码水平中心 X(用于扫码文案 g_south 居中)
  171. */
  172. private static function qrCenterX()
  173. {
  174. return self::POSTER_W - self::QR_PAD_RIGHT - (int)(self::QR_SIZE / 2);
  175. }
  176. /**
  177. * 扫码提示文案:位于太阳码正上方居中(g_south = 文字下边中点对齐)
  178. */
  179. private static function scanHintYFromBottom()
  180. {
  181. return self::QR_PAD_BOTTOM + self::QR_SIZE + self::QR_HINT_GAP;
  182. }
  183. /**
  184. * 动态白底画布(blank.png → 750 × totalH)
  185. */
  186. public static function buildCanvasBaseUrl($prefix, $totalH)
  187. {
  188. $absoluteMin = self::FOOTER_H + 120;
  189. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, (int)$totalH));
  190. $process = '?x-oss-process=image/resize,m_pad,w_' . self::POSTER_W . ',h_' . $totalH
  191. . ',color_' . self::CANVAS_COLOR;
  192. return $prefix . self::BLANK_CANVAS . $process;
  193. }
  194. /**
  195. * 和炫鲜花等门店角标
  196. */
  197. public static function buildHyxhLogoWatermark($mainId)
  198. {
  199. if (!in_array((int)$mainId, [52, 1459, 13495], true)) {
  200. return '';
  201. }
  202. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  203. if ($mainId == 1459) {
  204. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  205. }
  206. if ($mainId == 13495) {
  207. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  208. }
  209. return stringUtil::ossBase64($logo);
  210. }
  211. /**
  212. * 花束海报
  213. */
  214. public static function buildMallGoodsPosterUrl($prefix, array $options)
  215. {
  216. return self::buildMallSharePosterUrl($prefix, $options);
  217. }
  218. /**
  219. * 花材海报(商城散客)
  220. */
  221. public static function buildMallItemPosterUrl($prefix, array $options)
  222. {
  223. return self::buildMallSharePosterUrl($prefix, $options);
  224. }
  225. /**
  226. * 花农端花店采购海报(ghs 花材详情太阳码)
  227. */
  228. public static function buildGhsHdItemPosterUrl($prefix, array $options)
  229. {
  230. return self::buildMallSharePosterUrl($prefix, $options);
  231. }
  232. /**
  233. * 散客/花束分享海报:动态画布 + 主图槽位 + 底部信息区
  234. */
  235. private static function buildMallSharePosterUrl($prefix, array $options)
  236. {
  237. $cover = $options['cover'] ?? '';
  238. $miniCode = $options['miniCode'] ?? '';
  239. $priceText = $options['priceText'] ?? '';
  240. $name = $options['name'] ?? '';
  241. $staffText = $options['staffText'] ?? '';
  242. $mainId = (int)($options['mainId'] ?? 0);
  243. $flowerNum = (int)($options['flowerNum'] ?? 0);
  244. $subtitle = trim((string)($options['subtitle'] ?? ''));
  245. $scanHint = trim((string)($options['scanHint'] ?? self::SCAN_HINT));
  246. if ($scanHint === '微信扫码采购') {
  247. $scanHint = self::SCAN_HINT;
  248. }
  249. $coverW = (int)($options['coverWidth'] ?? 0);
  250. $coverH = (int)($options['coverHeight'] ?? 0);
  251. if ($coverW <= 0 || $coverH <= 0) {
  252. list($coverW, $coverH) = imgUtil::getOssImageSize($cover, $prefix);
  253. }
  254. $layout = self::calcPosterLayout($coverW, $coverH);
  255. $imgH = $layout['imgH'];
  256. $totalH = $layout['totalH'];
  257. $displayName = self::buildPosterNameText($name, $flowerNum, $subtitle);
  258. $nameLineCount = ($displayName !== '' && strpos($displayName, "\n") !== false) ? 2 : 1;
  259. $footerY = self::footerTextYPositions($nameLineCount);
  260. $coverWatermark = self::buildCoverWatermarkPath($cover, $layout['scaledH']);
  261. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_' . self::QR_SIZE . ',w_' . self::QR_SIZE;
  262. $url = self::buildCanvasBaseUrl($prefix, $totalH);
  263. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark)
  264. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . self::CANVAS_PAD_Y;
  265. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark) . ',g_se,x_' . self::QR_PAD_RIGHT . ',y_' . self::QR_PAD_BOTTOM;
  266. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  267. if ($logoB64 !== '') {
  268. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_210,y_48';
  269. }
  270. $url .= '/watermark,text_' . stringUtil::ossBase64($staffText)
  271. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footerY['staff']
  272. . ',color_999999,type_' . self::FONT . ',size_' . self::FOOTER_STAFF_SIZE;
  273. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText)
  274. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footerY['price']
  275. . ',color_EE2C2C,type_' . self::FONT . ',size_' . self::FOOTER_PRICE_SIZE;
  276. $url .= '/watermark,text_' . stringUtil::ossBase64($displayName)
  277. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footerY['name']
  278. . ',color_333333,type_' . self::FONT . ',size_' . self::FOOTER_NAME_SIZE;
  279. $url .= '/watermark,text_' . stringUtil::ossBase64($scanHint)
  280. . ',g_south,x_' . self::qrCenterX() . ',y_' . self::scanHintYFromBottom()
  281. . ',color_888888,type_' . self::FONT . ',size_24';
  282. return $url;
  283. }
  284. }