PosterUtil.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 = 42;
  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 = 30;
  38. const FOOTER_TEXT_GAP = 12;
  39. const FOOTER_PRICE_SIZE = 72;
  40. /**
  41. * 与 admin/home/me 一致:门店展示名、Logo 对象路径、itemRemark
  42. */
  43. public static function withShopAndRemark(array $options, $shop, $sj, $itemOrRemark = null)
  44. {
  45. $meta = self::resolveShopPosterMeta($shop, $sj);
  46. $options['shopTitle'] = $meta['shopTitle'];
  47. $options['shopLogo'] = $meta['shopLogo'];
  48. if (is_string($itemOrRemark)) {
  49. $options['itemRemark'] = self::filterPosterUtf8Text($itemOrRemark);
  50. } elseif ($itemOrRemark !== null) {
  51. $options['itemRemark'] = self::filterPosterUtf8Text($itemOrRemark->itemRemark ?? '');
  52. } else {
  53. $options['itemRemark'] = self::filterPosterUtf8Text($options['itemRemark'] ?? '');
  54. }
  55. return $options;
  56. }
  57. /**
  58. * hdApp/ghsApp me 页门店标题:sjName·shopName(首店仅 sjName)
  59. */
  60. public static function buildShopDisplayTitle($sjName, $shopName)
  61. {
  62. $sjName = trim((string)$sjName);
  63. $shopName = trim((string)$shopName);
  64. if ($shopName === '' || $shopName === '首店') {
  65. return $sjName;
  66. }
  67. return $sjName . '·' . $shopName;
  68. }
  69. public static function resolveShopPosterMeta($shop, $sj = null)
  70. {
  71. $sjName = '';
  72. if ($shop) {
  73. $sjName = trim((string)($shop->merchantName ?? ''));
  74. }
  75. if ($sj !== null) {
  76. if (is_array($sj)) {
  77. $sjName = trim((string)($sj['name'] ?? $sjName));
  78. } elseif (is_object($sj) && isset($sj->name)) {
  79. $sjName = trim((string)$sj->name);
  80. }
  81. }
  82. $shopName = $shop ? trim((string)($shop->shopName ?? '')) : '';
  83. $title = self::filterPosterUtf8Text(self::buildShopDisplayTitle($sjName, $shopName));
  84. if (mb_strlen($title, 'UTF-8') > self::SHOP_TITLE_MAX_LEN) {
  85. $title = mb_substr($title, 0, self::SHOP_TITLE_MAX_LEN, 'UTF-8') . '…';
  86. }
  87. return [
  88. 'shopTitle' => $title,
  89. 'shopLogo' => self::resolveShopLogoObjectPath($shop),
  90. ];
  91. }
  92. public static function resolveShopLogoObjectPath($shop)
  93. {
  94. $avatar = '';
  95. if ($shop && !empty($shop->avatar)) {
  96. $avatar = $shop->avatar;
  97. }
  98. return imgUtil::normalizeOssObjectPath($avatar);
  99. }
  100. /**
  101. * 海报文案 UTF-8 清洗:剔除非法字节、替换符及常见乱码字符
  102. */
  103. public static function filterPosterUtf8Text($text)
  104. {
  105. $text = trim((string)$text);
  106. if ($text === '') {
  107. return '';
  108. }
  109. if (function_exists('iconv')) {
  110. $fixed = @iconv('UTF-8', 'UTF-8//IGNORE', $text);
  111. if ($fixed !== false) {
  112. $text = $fixed;
  113. }
  114. }
  115. if (function_exists('mb_convert_encoding')) {
  116. $converted = @mb_convert_encoding($text, 'UTF-8', 'UTF-8');
  117. if (is_string($converted) && $converted !== '') {
  118. $text = $converted;
  119. }
  120. }
  121. $text = preg_replace('/[\x{FFFD}\x{FFF0}-\x{FFFF}]/u', '', $text);
  122. $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $text);
  123. $text = preg_replace(
  124. '/[^\x{4e00}-\x{9fff}a-zA-Z0-9\s,。、!?:;…·~%()【】\[\]\/\-_+|《》「」\'\"\.#&@\x{3000}-\x{303F}\x{FF01}-\x{FF5E}]/u',
  125. '',
  126. $text
  127. );
  128. $text = trim(preg_replace('/\s+/u', ' ', $text));
  129. return $text;
  130. }
  131. public static function truncateText($text, $maxLen = 14)
  132. {
  133. $text = self::filterPosterUtf8Text($text);
  134. if ($text === '') {
  135. return '';
  136. }
  137. if (mb_strlen($text, 'UTF-8') > $maxLen) {
  138. return mb_substr($text, 0, $maxLen, 'UTF-8') . '…';
  139. }
  140. return $text;
  141. }
  142. /**
  143. * @return array{0:string,1:string}
  144. */
  145. public static function splitPosterTextLines($text, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  146. {
  147. $text = self::filterPosterUtf8Text($text);
  148. if ($text === '') {
  149. return ['', ''];
  150. }
  151. $maxPerLine = (int)$maxPerLine;
  152. if ($maxPerLine < 1) {
  153. $maxPerLine = self::TEXT_CHARS_PER_LINE;
  154. }
  155. $len = mb_strlen($text, 'UTF-8');
  156. if ($len <= $maxPerLine) {
  157. return [$text, ''];
  158. }
  159. $line1 = mb_substr($text, 0, $maxPerLine, 'UTF-8');
  160. $rest = mb_substr($text, $maxPerLine, null, 'UTF-8');
  161. if (mb_strlen($rest, 'UTF-8') <= $maxPerLine) {
  162. return [$line1, $rest];
  163. }
  164. $line2 = mb_substr($rest, 0, $maxPerLine, 'UTF-8') . '…';
  165. return [$line1, $line2];
  166. }
  167. /** @deprecated 兼容旧调用 */
  168. public static function splitPosterNameLines($name, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  169. {
  170. return self::splitPosterTextLines($name, $maxPerLine);
  171. }
  172. public static function buildPosterTwoLineText($text, $maxPerLine = self::TEXT_CHARS_PER_LINE)
  173. {
  174. list($line1, $line2) = self::splitPosterTextLines($text, $maxPerLine);
  175. if ($line2 === '') {
  176. return $line1;
  177. }
  178. return $line1 . "\n" . $line2;
  179. }
  180. /**
  181. * 商品名(图下,最多两行)
  182. */
  183. public static function buildPosterNameText($name, $flowerNum = 0, $subtitle = '')
  184. {
  185. $name = self::filterPosterUtf8Text($name);
  186. $subtitle = self::filterPosterUtf8Text($subtitle);
  187. list($line1, $line2) = self::splitPosterTextLines($name);
  188. $suffix = '';
  189. if ($flowerNum > 0) {
  190. $suffix = ' · ' . (int)$flowerNum . '支';
  191. } elseif (trim((string)$subtitle) !== '') {
  192. $suffix = ' · ' . trim((string)$subtitle);
  193. }
  194. if ($suffix !== '') {
  195. if ($line2 === '') {
  196. $merged = $line1 . $suffix;
  197. if (mb_strlen($merged, 'UTF-8') <= self::TEXT_CHARS_PER_LINE) {
  198. $line1 = $merged;
  199. } else {
  200. $line2 = self::truncateText($suffix, self::TEXT_CHARS_PER_LINE);
  201. }
  202. } else {
  203. $merged = $line2 . $suffix;
  204. $line2 = mb_strlen($merged, 'UTF-8') <= self::TEXT_CHARS_PER_LINE
  205. ? $merged
  206. : self::truncateText($merged, self::TEXT_CHARS_PER_LINE);
  207. }
  208. }
  209. if ($line2 === '') {
  210. return $line1;
  211. }
  212. return $line1 . "\n" . $line2;
  213. }
  214. /**
  215. * 注意事项(图下、名称下,无内容返回空串)
  216. */
  217. public static function buildPosterRemarkText($itemRemark)
  218. {
  219. $itemRemark = self::filterPosterUtf8Text($itemRemark);
  220. if ($itemRemark === '') {
  221. return '';
  222. }
  223. return self::buildPosterTwoLineText(self::REMARK_PREFIX . $itemRemark);
  224. }
  225. private static function countTextLines($text)
  226. {
  227. $text = trim((string)$text);
  228. if ($text === '') {
  229. return 0;
  230. }
  231. return substr_count($text, "\n") + 1;
  232. }
  233. private static function textBlockHeight($lineCount, $fontSize)
  234. {
  235. if ($lineCount <= 0) {
  236. return 0;
  237. }
  238. $lineH = (int)ceil($fontSize * 1.2);
  239. return $lineCount * $lineH + max(0, $lineCount - 1) * self::MID_LINE_GAP;
  240. }
  241. private static function calcMidSectionHeight($nameText, $remarkText)
  242. {
  243. $h = 0;
  244. $nameLines = self::countTextLines($nameText);
  245. if ($nameLines > 0) {
  246. $h += self::MID_AFTER_COVER_GAP + self::textBlockHeight($nameLines, self::MID_NAME_SIZE);
  247. }
  248. $remarkLines = self::countTextLines($remarkText);
  249. if ($remarkLines > 0) {
  250. $h += ($nameLines > 0 ? self::MID_BLOCK_GAP : self::MID_AFTER_COVER_GAP);
  251. $h += self::textBlockHeight($remarkLines, self::MID_REMARK_SIZE);
  252. }
  253. return $h;
  254. }
  255. private static function coverStartY()
  256. {
  257. return self::CANVAS_PAD_Y + self::SHOP_LOGO_SIZE + self::HEADER_BOTTOM_GAP;
  258. }
  259. /**
  260. * 底部左侧(价格 + 提示文案)与右侧太阳码垂直居中对齐,均左对齐
  261. *
  262. * @return array{buyHintY:int,priceY:int,qrY:int}
  263. */
  264. private static function calcFooterLayout()
  265. {
  266. $qrCenterFromBottom = self::QR_PAD_BOTTOM + (int)(self::QR_SIZE / 2);
  267. $buyHintH = (int)ceil(self::FOOTER_BUY_HINT_SIZE * 1.2);
  268. $priceH = (int)ceil(self::FOOTER_PRICE_SIZE * 1.3);
  269. $gap = self::FOOTER_TEXT_GAP;
  270. $blockH = $priceH + $gap + $buyHintH;
  271. $buyHintY = max(16, $qrCenterFromBottom - (int)($blockH / 2));
  272. $priceY = $buyHintY + $buyHintH + $gap;
  273. $qrY = max(16, $qrCenterFromBottom - (int)(self::QR_SIZE / 2));
  274. return [
  275. 'buyHintY' => $buyHintY,
  276. 'priceY' => $priceY,
  277. 'qrY' => $qrY,
  278. ];
  279. }
  280. /**
  281. * @return array{imgH:int,totalH:int,scaledH:int,posterW:int,coverY:int,nameY:int,remarkY:int}
  282. */
  283. public static function calcPosterLayout($coverWidth, $coverHeight, array $textOptions = [])
  284. {
  285. $coverWidth = (int)$coverWidth;
  286. $coverHeight = (int)$coverHeight;
  287. $scaledH = 0;
  288. if ($coverWidth > 0 && $coverHeight > 0) {
  289. $scaledH = (int)round($coverHeight * (self::SLOT_W / $coverWidth));
  290. $imgH = max(self::IMG_H_MIN, min(self::IMG_H_MAX, $scaledH));
  291. } else {
  292. $scaledH = self::IMG_H_MIN;
  293. $imgH = self::IMG_H_MIN;
  294. }
  295. $nameText = self::buildPosterNameText(
  296. $textOptions['name'] ?? '',
  297. (int)($textOptions['flowerNum'] ?? 0),
  298. $textOptions['subtitle'] ?? ''
  299. );
  300. $remarkText = self::buildPosterRemarkText($textOptions['itemRemark'] ?? '');
  301. $coverY = self::coverStartY();
  302. $midH = self::calcMidSectionHeight($nameText, $remarkText);
  303. $totalH = $coverY + $imgH + $midH + self::FOOTER_H;
  304. $absoluteMin = self::coverStartY() + self::IMG_H_MIN + self::FOOTER_H;
  305. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, $totalH));
  306. $nameY = $coverY + $imgH + ($nameText !== '' ? self::MID_AFTER_COVER_GAP : 0);
  307. $remarkY = 0;
  308. if ($remarkText !== '') {
  309. $remarkY = $nameY;
  310. if ($nameText !== '') {
  311. $remarkY += self::textBlockHeight(self::countTextLines($nameText), self::MID_NAME_SIZE);
  312. $remarkY += self::MID_BLOCK_GAP;
  313. } else {
  314. $remarkY = $coverY + $imgH + self::MID_AFTER_COVER_GAP;
  315. }
  316. }
  317. return [
  318. 'imgH' => $imgH,
  319. 'totalH' => $totalH,
  320. 'scaledH' => $scaledH,
  321. 'posterW' => self::POSTER_W,
  322. 'coverY' => $coverY,
  323. 'nameY' => $nameY,
  324. 'remarkY' => $remarkY,
  325. ];
  326. }
  327. private static function buildCoverWatermarkPath($cover, $scaledH)
  328. {
  329. $scaledH = (int)$scaledH;
  330. if ($scaledH > self::IMG_H_MAX) {
  331. return $cover . '?x-oss-process=image/resize,m_fill,w_' . self::SLOT_W . ',h_' . self::IMG_H_MAX . ',limit_0';
  332. }
  333. return $cover . '?x-oss-process=image/resize,m_lfit,w_' . self::SLOT_W . ',limit_0';
  334. }
  335. private static function buildShopLogoWatermarkPath($logoObject)
  336. {
  337. $logoObject = imgUtil::normalizeOssObjectPath($logoObject);
  338. return $logoObject . '?x-oss-process=image/resize,m_fill,h_' . self::SHOP_LOGO_SIZE
  339. . ',w_' . self::SHOP_LOGO_SIZE . ',limit_0';
  340. }
  341. private static function shopTitleY()
  342. {
  343. return self::CANVAS_PAD_Y + (int)((self::SHOP_LOGO_SIZE - self::SHOP_TITLE_SIZE) / 2);
  344. }
  345. public static function buildCanvasBaseUrl($prefix, $totalH)
  346. {
  347. $absoluteMin = self::coverStartY() + self::IMG_H_MIN + self::FOOTER_H;
  348. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, (int)$totalH));
  349. $process = '?x-oss-process=image/resize,m_pad,w_' . self::POSTER_W . ',h_' . $totalH
  350. . ',color_' . self::CANVAS_COLOR;
  351. return $prefix . self::BLANK_CANVAS . $process;
  352. }
  353. public static function buildHyxhLogoWatermark($mainId)
  354. {
  355. if (!in_array((int)$mainId, [52, 1459, 13495], true)) {
  356. return '';
  357. }
  358. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  359. if ($mainId == 1459) {
  360. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  361. }
  362. if ($mainId == 13495) {
  363. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  364. }
  365. return stringUtil::ossBase64($logo);
  366. }
  367. public static function buildMallGoodsPosterUrl($prefix, array $options)
  368. {
  369. return self::buildMallSharePosterUrl($prefix, $options);
  370. }
  371. public static function buildMallItemPosterUrl($prefix, array $options)
  372. {
  373. return self::buildMallSharePosterUrl($prefix, $options);
  374. }
  375. public static function buildGhsHdItemPosterUrl($prefix, array $options)
  376. {
  377. return self::buildMallSharePosterUrl($prefix, $options);
  378. }
  379. private static function buildMallSharePosterUrl($prefix, array $options)
  380. {
  381. $cover = $options['cover'] ?? '';
  382. $miniCode = $options['miniCode'] ?? '';
  383. $priceText = $options['priceText'] ?? '';
  384. $name = self::filterPosterUtf8Text($options['name'] ?? '');
  385. $mainId = (int)($options['mainId'] ?? 0);
  386. $flowerNum = (int)($options['flowerNum'] ?? 0);
  387. $subtitle = self::filterPosterUtf8Text($options['subtitle'] ?? '');
  388. $itemRemark = self::filterPosterUtf8Text($options['itemRemark'] ?? '');
  389. $shopTitle = self::filterPosterUtf8Text($options['shopTitle'] ?? '');
  390. $shopLogo = trim((string)($options['shopLogo'] ?? ''));
  391. $coverW = (int)($options['coverWidth'] ?? 0);
  392. $coverH = (int)($options['coverHeight'] ?? 0);
  393. if ($coverW <= 0 || $coverH <= 0) {
  394. list($coverW, $coverH) = imgUtil::getOssImageSize($cover, $prefix);
  395. }
  396. $layout = self::calcPosterLayout($coverW, $coverH, [
  397. 'name' => $name,
  398. 'flowerNum' => $flowerNum,
  399. 'subtitle' => $subtitle,
  400. 'itemRemark' => $itemRemark,
  401. ]);
  402. $displayName = self::buildPosterNameText($name, $flowerNum, $subtitle);
  403. $displayRemark = self::buildPosterRemarkText($itemRemark);
  404. $footer = self::calcFooterLayout();
  405. $coverWatermark = self::buildCoverWatermarkPath($cover, $layout['scaledH']);
  406. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_' . self::QR_SIZE . ',w_' . self::QR_SIZE;
  407. $url = self::buildCanvasBaseUrl($prefix, $layout['totalH']);
  408. if ($shopLogo !== '') {
  409. $url .= '/watermark,image_' . stringUtil::ossBase64(self::buildShopLogoWatermarkPath($shopLogo))
  410. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . self::CANVAS_PAD_Y;
  411. }
  412. if ($shopTitle !== '') {
  413. $titleX = self::CANVAS_PAD_X + self::SHOP_LOGO_SIZE + self::SHOP_LOGO_GAP;
  414. $url .= '/watermark,text_' . stringUtil::ossBase64($shopTitle)
  415. . ',g_nw,x_' . $titleX . ',y_' . self::shopTitleY()
  416. . ',color_333333,type_' . self::FONT . ',size_' . self::SHOP_TITLE_SIZE;
  417. }
  418. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark)
  419. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['coverY'];
  420. if ($displayName !== '') {
  421. $url .= '/watermark,text_' . stringUtil::ossBase64($displayName)
  422. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['nameY']
  423. . ',color_333333,type_' . self::FONT . ',size_' . self::MID_NAME_SIZE;
  424. }
  425. if ($displayRemark !== '') {
  426. $url .= '/watermark,text_' . stringUtil::ossBase64($displayRemark)
  427. . ',g_nw,x_' . self::CANVAS_PAD_X . ',y_' . $layout['remarkY']
  428. . ',color_888888,type_' . self::FONT . ',size_' . self::MID_REMARK_SIZE;
  429. }
  430. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark)
  431. . ',g_se,x_' . self::QR_PAD_RIGHT . ',y_' . $footer['qrY'];
  432. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  433. if ($logoB64 !== '') {
  434. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_210,y_48';
  435. }
  436. $url .= '/watermark,text_' . stringUtil::ossBase64(self::FOOTER_BUY_HINT)
  437. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footer['buyHintY']
  438. . ',color_888888,type_' . self::FONT . ',size_' . self::FOOTER_BUY_HINT_SIZE;
  439. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText)
  440. . ',g_sw,x_' . self::CANVAS_PAD_X . ',y_' . $footer['priceY']
  441. . ',color_EE2C2C,type_' . self::FONT . ',size_' . self::FOOTER_PRICE_SIZE;
  442. return $url;
  443. }
  444. }