PosterUtil.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace common\components;
  3. /**
  4. * 散客分享海报(OSS:poster/blank.png 动态画布 + 水印合成,布局参考快团团)
  5. */
  6. class PosterUtil
  7. {
  8. const BLANK_CANVAS = 'poster/blank.png';
  9. const FONT = 'd3F5LW1pY3JvaGVp';
  10. const POSTER_W = 750;
  11. const CANVAS_PAD_X = 20;
  12. const CANVAS_PAD_Y = 20;
  13. /** 文案每行最多字数(商品名、注意事项) */
  14. const TEXT_CHARS_PER_LINE = 13;
  15. const SLOT_W = 710;
  16. const FOOTER_H = 300;
  17. const IMG_H_MIN = 240;
  18. const IMG_H_MAX = 880;
  19. const TOTAL_H_MAX = 2000;
  20. const CANVAS_COLOR = 'FFFFFF';
  21. const SHOP_LOGO_SIZE = 72;
  22. const SHOP_LOGO_GAP = 12;
  23. const SHOP_TITLE_SIZE = 36;
  24. const SHOP_TITLE_MAX_LEN = 18;
  25. const HEADER_BOTTOM_GAP = 16;
  26. const MID_AFTER_COVER_GAP = 16;
  27. const MID_BLOCK_GAP = 10;
  28. const MID_LINE_GAP = 6;
  29. const MID_NAME_SIZE = 32;
  30. const MID_REMARK_SIZE = 26;
  31. const REMARK_PREFIX = '注意:';
  32. const QR_SIZE = 220;
  33. const QR_PAD_RIGHT = 36;
  34. const QR_PAD_BOTTOM = 32;
  35. /** 价格下方:长按识别提示(原二维码旁「微信扫码下单」已取消) */
  36. const FOOTER_BUY_HINT = '长按识别小程序购买';
  37. const FOOTER_BUY_HINT_SIZE = 26;
  38. const FOOTER_BUY_HINT_SHADOW = 55;
  39. const FOOTER_BUY_HINT_Y = 24;
  40. const FOOTER_TEXT_GAP = 12;
  41. const FOOTER_PRICE_SIZE = 56;
  42. /**
  43. * 与 admin/home/me 一致:门店展示名、Logo 对象路径、itemRemark
  44. */
  45. public static function withShopAndRemark(array $options, $shop, $sj, $itemOrRemark = null)
  46. {
  47. $meta = self::resolveShopPosterMeta($shop, $sj);
  48. $options['shopTitle'] = $meta['shopTitle'];
  49. $options['shopLogo'] = $meta['shopLogo'];
  50. if (is_string($itemOrRemark)) {
  51. $options['itemRemark'] = trim($itemOrRemark);
  52. } elseif ($itemOrRemark !== null) {
  53. $options['itemRemark'] = trim($itemOrRemark->itemRemark ?? '');
  54. } else {
  55. $options['itemRemark'] = trim($options['itemRemark'] ?? '');
  56. }
  57. return $options;
  58. }
  59. /**
  60. * hdApp/ghsApp me 页门店标题:sjName·shopName(首店仅 sjName)
  61. */
  62. public static function buildShopDisplayTitle($sjName, $shopName)
  63. {
  64. $sjName = trim((string)$sjName);
  65. $shopName = trim((string)$shopName);
  66. if ($shopName === '' || $shopName === '首店') {
  67. return $sjName;
  68. }
  69. return $sjName . '·' . $shopName;
  70. }
  71. public static function resolveShopPosterMeta($shop, $sj = null)
  72. {
  73. $sjName = '';
  74. if ($shop) {
  75. $sjName = trim((string)($shop->merchantName ?? ''));
  76. }
  77. if ($sj !== null) {
  78. if (is_array($sj)) {
  79. $sjName = trim((string)($sj['name'] ?? $sjName));
  80. } elseif (is_object($sj) && isset($sj->name)) {
  81. $sjName = trim((string)$sj->name);
  82. }
  83. }
  84. $shopName = $shop ? trim((string)($shop->shopName ?? '')) : '';
  85. $title = self::buildShopDisplayTitle($sjName, $shopName);
  86. if (mb_strlen($title, 'UTF-8') > self::SHOP_TITLE_MAX_LEN) {
  87. $title = mb_substr($title, 0, self::SHOP_TITLE_MAX_LEN, 'UTF-8') . '…';
  88. }
  89. return [
  90. 'shopTitle' => $title,
  91. 'shopLogo' => self::resolveShopLogoObjectPath($shop),
  92. ];
  93. }
  94. public static function resolveShopLogoObjectPath($shop)
  95. {
  96. $avatar = '';
  97. if ($shop && !empty($shop->avatar)) {
  98. $avatar = $shop->avatar;
  99. }
  100. return imgUtil::normalizeOssObjectPath($avatar);
  101. }
  102. public static function truncateText($text, $maxLen = 14)
  103. {
  104. $text = trim((string)$text);
  105. if ($text === '') {
  106. return '';
  107. }
  108. if (mb_strlen($text, 'UTF-8') > $maxLen) {
  109. return mb_substr($text, 0, $maxLen, 'UTF-8') . '…';
  110. }
  111. return $text;
  112. }
  113. /**
  114. * @return array{0:string,1:string}
  115. */
  116. public static function splitPosterTextLines($text, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  117. {
  118. $text = trim((string)$text);
  119. if ($text === '') {
  120. return ['', ''];
  121. }
  122. $maxPerLine = (int)$maxPerLine;
  123. if ($maxPerLine < 1) {
  124. $maxPerLine = self::TEXT_CHARS_PER_LINE;
  125. }
  126. $len = mb_strlen($text, 'UTF-8');
  127. if ($len <= $maxPerLine) {
  128. return [$text, ''];
  129. }
  130. $line1 = mb_substr($text, 0, $maxPerLine, 'UTF-8');
  131. $rest = mb_substr($text, $maxPerLine, null, 'UTF-8');
  132. if (mb_strlen($rest, 'UTF-8') <= $maxPerLine) {
  133. return [$line1, $rest];
  134. }
  135. $line2 = mb_substr($rest, 0, $maxPerLine, 'UTF-8') . '…';
  136. return [$line1, $line2];
  137. }
  138. /** @deprecated 兼容旧调用 */
  139. public static function splitPosterNameLines($name, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  140. {
  141. return self::splitPosterTextLines($name, $maxPerLine);
  142. }
  143. public static function buildPosterTwoLineText($text, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  144. {
  145. list($line1, $line2) = self::splitPosterTextLines($text, $maxPerLine);
  146. if ($line2 === '') {
  147. return $line1;
  148. }
  149. return $line1 . "\n" . $line2;
  150. }
  151. /**
  152. * 商品名(图下,最多两行)
  153. */
  154. public static function buildPosterNameText($name, $flowerNum = 0, $subtitle = '')
  155. {
  156. list($line1, $line2) = self::splitPosterTextLines($name);
  157. $suffix = '';
  158. if ($flowerNum > 0) {
  159. $suffix = ' · ' . (int)$flowerNum . '支';
  160. } elseif (trim((string)$subtitle) !== '') {
  161. $suffix = ' · ' . trim((string)$subtitle);
  162. }
  163. if ($suffix !== '') {
  164. if ($line2 === '') {
  165. $merged = $line1 . $suffix;
  166. if (mb_strlen($merged, 'UTF-8') <= self::TEXT_CHARS_PER_LINE) {
  167. $line1 = $merged;
  168. } else {
  169. $line2 = self::truncateText($suffix, self::TEXT_CHARS_PER_LINE);
  170. }
  171. } else {
  172. $merged = $line2 . $suffix;
  173. $line2 = mb_strlen($merged, 'UTF-8') <= self::TEXT_CHARS_PER_LINE
  174. ? $merged
  175. : self::truncateText($merged, self::TEXT_CHARS_PER_LINE);
  176. }
  177. }
  178. if ($line2 === '') {
  179. return $line1;
  180. }
  181. return $line1 . "\n" . $line2;
  182. }
  183. /**
  184. * 注意事项(图下、名称下,无内容返回空串)
  185. */
  186. public static function buildPosterRemarkText($itemRemark)
  187. {
  188. $itemRemark = trim((string)$itemRemark);
  189. if ($itemRemark === '') {
  190. return '';
  191. }
  192. return self::buildPosterTwoLineText(self::REMARK_PREFIX . $itemRemark);
  193. }
  194. private static function countTextLines($text)
  195. {
  196. $text = trim((string)$text);
  197. if ($text === '') {
  198. return 0;
  199. }
  200. return substr_count($text, "\n") + 1;
  201. }
  202. private static function textBlockHeight($lineCount, $fontSize)
  203. {
  204. if ($lineCount <= 0) {
  205. return 0;
  206. }
  207. $lineH = (int)ceil($fontSize * 1.2);
  208. return $lineCount * $lineH + max(0, $lineCount - 1) * self::MID_LINE_GAP;
  209. }
  210. private static function calcMidSectionHeight($nameText, $remarkText)
  211. {
  212. $h = 0;
  213. $nameLines = self::countTextLines($nameText);
  214. if ($nameLines > 0) {
  215. $h += self::MID_AFTER_COVER_GAP + self::textBlockHeight($nameLines, self::MID_NAME_SIZE);
  216. }
  217. $remarkLines = self::countTextLines($remarkText);
  218. if ($remarkLines > 0) {
  219. $h += ($nameLines > 0 ? self::MID_BLOCK_GAP : self::MID_AFTER_COVER_GAP);
  220. $h += self::textBlockHeight($remarkLines, self::MID_REMARK_SIZE);
  221. }
  222. return $h;
  223. }
  224. private static function coverStartY()
  225. {
  226. return self::CANVAS_PAD_Y + self::SHOP_LOGO_SIZE + self::HEADER_BOTTOM_GAP;
  227. }
  228. private static function footerTextYPositions()
  229. {
  230. $buyHintH = (int)ceil(self::FOOTER_BUY_HINT_SIZE * 1.25);
  231. $buyHintY = self::FOOTER_BUY_HINT_Y;
  232. $priceY = $buyHintY + $buyHintH + self::FOOTER_TEXT_GAP;
  233. return [
  234. 'buyHint' => $buyHintY,
  235. 'price' => $priceY,
  236. ];
  237. }
  238. /**
  239. * @return array{imgH:int,totalH:int,scaledH:int,posterW:int,coverY:int,nameY:int,remarkY:int}
  240. */
  241. public static function calcPosterLayout($coverWidth, $coverHeight, array $textOptions = [])
  242. {
  243. $coverWidth = (int)$coverWidth;
  244. $coverHeight = (int)$coverHeight;
  245. $scaledH = 0;
  246. if ($coverWidth > 0 && $coverHeight > 0) {
  247. $scaledH = (int)round($coverHeight * (self::SLOT_W / $coverWidth));
  248. $imgH = max(self::IMG_H_MIN, min(self::IMG_H_MAX, $scaledH));
  249. } else {
  250. $scaledH = self::IMG_H_MIN;
  251. $imgH = self::IMG_H_MIN;
  252. }
  253. $nameText = self::buildPosterNameText(
  254. $textOptions['name'] ?? '',
  255. (int)($textOptions['flowerNum'] ?? 0),
  256. $textOptions['subtitle'] ?? ''
  257. );
  258. $remarkText = self::buildPosterRemarkText($textOptions['itemRemark'] ?? '');
  259. $coverY = self::coverStartY();
  260. $midH = self::calcMidSectionHeight($nameText, $remarkText);
  261. $totalH = $coverY + $imgH + $midH + self::FOOTER_H;
  262. $absoluteMin = self::coverStartY() + self::IMG_H_MIN + self::FOOTER_H;
  263. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, $totalH));
  264. $nameY = $coverY + $imgH + ($nameText !== '' ? self::MID_AFTER_COVER_GAP : 0);
  265. $remarkY = 0;
  266. if ($remarkText !== '') {
  267. $remarkY = $nameY;
  268. if ($nameText !== '') {
  269. $remarkY += self::textBlockHeight(self::countTextLines($nameText), self::MID_NAME_SIZE);
  270. $remarkY += self::MID_BLOCK_GAP;
  271. } else {
  272. $remarkY = $coverY + $imgH + self::MID_AFTER_COVER_GAP;
  273. }
  274. }
  275. return [
  276. 'imgH' => $imgH,
  277. 'totalH' => $totalH,
  278. 'scaledH' => $scaledH,
  279. 'posterW' => self::POSTER_W,
  280. 'coverY' => $coverY,
  281. 'nameY' => $nameY,
  282. 'remarkY' => $remarkY,
  283. ];
  284. }
  285. private static function buildCoverWatermarkPath($cover, $scaledH)
  286. {
  287. $scaledH = (int)$scaledH;
  288. if ($scaledH > self::IMG_H_MAX) {
  289. return $cover . '?x-oss-process=image/resize,m_fill,w_' . self::SLOT_W . ',h_' . self::IMG_H_MAX . ',limit_0';
  290. }
  291. return $cover . '?x-oss-process=image/resize,m_lfit,w_' . self::SLOT_W . ',limit_0';
  292. }
  293. private static function buildShopLogoWatermarkPath($logoObject)
  294. {
  295. $logoObject = imgUtil::normalizeOssObjectPath($logoObject);
  296. return $logoObject . '?x-oss-process=image/resize,m_fill,h_' . self::SHOP_LOGO_SIZE
  297. . ',w_' . self::SHOP_LOGO_SIZE . ',limit_0';
  298. }
  299. private static function shopTitleY()
  300. {
  301. return self::CANVAS_PAD_Y + (int)((self::SHOP_LOGO_SIZE - self::SHOP_TITLE_SIZE) / 2);
  302. }
  303. public static function buildCanvasBaseUrl($prefix, $totalH)
  304. {
  305. $absoluteMin = self::coverStartY() + self::IMG_H_MIN + self::FOOTER_H;
  306. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, (int)$totalH));
  307. $process = '?x-oss-process=image/resize,m_pad,w_' . self::POSTER_W . ',h_' . $totalH
  308. . ',color_' . self::CANVAS_COLOR;
  309. return $prefix . self::BLANK_CANVAS . $process;
  310. }
  311. public static function buildHyxhLogoWatermark($mainId)
  312. {
  313. if (!in_array((int)$mainId, [52, 1459, 13495], true)) {
  314. return '';
  315. }
  316. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  317. if ($mainId == 1459) {
  318. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  319. }
  320. if ($mainId == 13495) {
  321. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  322. }
  323. return stringUtil::ossBase64($logo);
  324. }
  325. public static function buildMallGoodsPosterUrl($prefix, array $options)
  326. {
  327. return self::buildMallSharePosterUrl($prefix, $options);
  328. }
  329. public static function buildMallItemPosterUrl($prefix, array $options)
  330. {
  331. return self::buildMallSharePosterUrl($prefix, $options);
  332. }
  333. public static function buildGhsHdItemPosterUrl($prefix, array $options)
  334. {
  335. return self::buildMallSharePosterUrl($prefix, $options);
  336. }
  337. private static function buildMallSharePosterUrl($prefix, array $options)
  338. {
  339. $cover = $options['cover'] ?? '';
  340. $miniCode = $options['miniCode'] ?? '';
  341. $priceText = $options['priceText'] ?? '';
  342. $name = $options['name'] ?? '';
  343. $mainId = (int)($options['mainId'] ?? 0);
  344. $flowerNum = (int)($options['flowerNum'] ?? 0);
  345. $subtitle = trim((string)($options['subtitle'] ?? ''));
  346. $itemRemark = trim((string)($options['itemRemark'] ?? ''));
  347. $shopTitle = trim((string)($options['shopTitle'] ?? ''));
  348. $shopLogo = trim((string)($options['shopLogo'] ?? ''));
  349. $coverW = (int)($options['coverWidth'] ?? 0);
  350. $coverH = (int)($options['coverHeight'] ?? 0);
  351. if ($coverW <= 0 || $coverH <= 0) {
  352. list($coverW, $coverH) = imgUtil::getOssImageSize($cover, $prefix);
  353. }
  354. $layout = self::calcPosterLayout($coverW, $coverH, [
  355. 'name' => $name,
  356. 'flowerNum' => $flowerNum,
  357. 'subtitle' => $subtitle,
  358. 'itemRemark' => $itemRemark,
  359. ]);
  360. $displayName = self::buildPosterNameText($name, $flowerNum, $subtitle);
  361. $displayRemark = self::buildPosterRemarkText($itemRemark);
  362. $footerY = self::footerTextYPositions();
  363. $coverWatermark = self::buildCoverWatermarkPath($cover, $layout['scaledH']);
  364. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_' . self::QR_SIZE . ',w_' . self::QR_SIZE;
  365. $url = self::buildCanvasBaseUrl($prefix, $layout['totalH']);
  366. if ($shopLogo !== '') {
  367. $url .= '/watermark,image_' . stringUtil::ossBase64(self::buildShopLogoWatermarkPath($shopLogo))
  368. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . self::CANVAS_PAD_Y;
  369. }
  370. if ($shopTitle !== '') {
  371. $titleX = self::CANVAS_PAD_X + self::SHOP_LOGO_SIZE + self::SHOP_LOGO_GAP;
  372. $url .= '/watermark,text_' . stringUtil::ossBase64($shopTitle)
  373. . ',g_nw,x_' . $titleX . ',y_' . self::shopTitleY()
  374. . ',color_333333,type_' . self::FONT . ',size_' . self::SHOP_TITLE_SIZE;
  375. }
  376. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark)
  377. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['coverY'];
  378. if ($displayName !== '') {
  379. $url .= '/watermark,text_' . stringUtil::ossBase64($displayName)
  380. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['nameY']
  381. . ',color_333333,type_' . self::FONT . ',size_' . self::MID_NAME_SIZE;
  382. }
  383. if ($displayRemark !== '') {
  384. $url .= '/watermark,text_' . stringUtil::ossBase64($displayRemark)
  385. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['remarkY']
  386. . ',color_888888,type_' . self::FONT . ',size_' . self::MID_REMARK_SIZE;
  387. }
  388. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark)
  389. . ',g_se,x_' . self::QR_PAD_RIGHT . ',y_' . self::QR_PAD_BOTTOM;
  390. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  391. if ($logoB64 !== '') {
  392. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_210,y_48';
  393. }
  394. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText)
  395. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footerY['price']
  396. . ',color_EE2C2C,type_' . self::FONT . ',size_' . self::FOOTER_PRICE_SIZE;
  397. $url .= '/watermark,text_' . stringUtil::ossBase64(self::FOOTER_BUY_HINT)
  398. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footerY['buyHint']
  399. . ',color_888888,type_' . self::FONT . ',size_' . self::FOOTER_BUY_HINT_SIZE
  400. . ',shadow_' . self::FOOTER_BUY_HINT_SHADOW;
  401. return $url;
  402. }
  403. }