PosterUtil.php 18 KB

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