PosterUtil.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. const SLOT_W = 700;
  13. /** 底部价格/标题/太阳码固定区 */
  14. const FOOTER_H = 360;
  15. /** 主图区温和下限,避免扁图海报过矮 */
  16. const IMG_H_MIN = 240;
  17. const IMG_H_MAX = 880;
  18. const TOTAL_H_MAX = 1600;
  19. const CANVAS_COLOR = 'FFFFFF';
  20. const QR_SIZE = 220;
  21. const QR_PAD_RIGHT = 36;
  22. const QR_PAD_BOTTOM = 32;
  23. const QR_HINT_GAP = 14;
  24. const SCAN_HINT = '微信扫码下单';
  25. /**
  26. * 海报标题过长时截断,避免压住底部文案区
  27. */
  28. public static function truncateText($text, $maxLen = 14)
  29. {
  30. $text = trim((string)$text);
  31. if ($text === '') {
  32. return '';
  33. }
  34. if (mb_strlen($text, 'UTF-8') > $maxLen) {
  35. return mb_substr($text, 0, $maxLen, 'UTF-8') . '…';
  36. }
  37. return $text;
  38. }
  39. /**
  40. * 按商品图比例计算主图区高度与画布总高(能多矮就多矮,仅保留温和下限)
  41. *
  42. * @return array{imgH:int,totalH:int,scaledH:int,posterW:int}
  43. */
  44. public static function calcPosterLayout($coverWidth, $coverHeight)
  45. {
  46. $coverWidth = (int)$coverWidth;
  47. $coverHeight = (int)$coverHeight;
  48. $scaledH = 0;
  49. if ($coverWidth > 0 && $coverHeight > 0) {
  50. $scaledH = (int)round($coverHeight * (self::SLOT_W / $coverWidth));
  51. $imgH = max(self::IMG_H_MIN, min(self::IMG_H_MAX, $scaledH));
  52. } else {
  53. $imgH = self::IMG_H_MIN;
  54. $scaledH = $imgH;
  55. }
  56. $totalH = $imgH + self::FOOTER_H;
  57. $absoluteMin = self::FOOTER_H + 120;
  58. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, $totalH));
  59. return [
  60. 'imgH' => $imgH,
  61. 'totalH' => $totalH,
  62. 'scaledH' => $scaledH,
  63. 'posterW' => self::POSTER_W,
  64. ];
  65. }
  66. /**
  67. * 短图/扁图:lfit 后 pad;长图:m_fill 裁切进槽位
  68. */
  69. private static function buildCoverWatermarkPath($cover, $imgH, $scaledH, $coverWidth, $coverHeight)
  70. {
  71. $wide = $coverWidth > 0 && $coverHeight > 0 && ($coverWidth / $coverHeight) > 1.15;
  72. $shortSlot = $scaledH > 0 && ($scaledH < $imgH || $wide);
  73. if ($shortSlot) {
  74. return $cover . '?x-oss-process=image/resize,m_lfit,h_' . $imgH . ',w_' . self::SLOT_W . ',limit_0'
  75. . '/resize,m_pad,h_' . $imgH . ',w_' . self::SLOT_W . ',color_' . self::CANVAS_COLOR;
  76. }
  77. return $cover . '?x-oss-process=image/resize,m_fill,h_' . $imgH . ',w_' . self::SLOT_W;
  78. }
  79. /**
  80. * 太阳码水平中心 X(用于扫码文案 g_south 居中)
  81. */
  82. private static function qrCenterX()
  83. {
  84. return self::POSTER_W - self::QR_PAD_RIGHT - (int)(self::QR_SIZE / 2);
  85. }
  86. /**
  87. * 扫码提示文案:位于太阳码正上方居中(g_south = 文字下边中点对齐)
  88. */
  89. private static function scanHintYFromBottom()
  90. {
  91. return self::QR_PAD_BOTTOM + self::QR_SIZE + self::QR_HINT_GAP;
  92. }
  93. /**
  94. * 动态白底画布(blank.png → 750 × totalH)
  95. */
  96. public static function buildCanvasBaseUrl($prefix, $totalH)
  97. {
  98. $absoluteMin = self::FOOTER_H + 120;
  99. $totalH = max($absoluteMin, min(self::TOTAL_H_MAX, (int)$totalH));
  100. $process = '?x-oss-process=image/resize,m_pad,w_' . self::POSTER_W . ',h_' . $totalH
  101. . ',color_' . self::CANVAS_COLOR;
  102. return $prefix . self::BLANK_CANVAS . $process;
  103. }
  104. /**
  105. * 和炫鲜花等门店角标
  106. */
  107. public static function buildHyxhLogoWatermark($mainId)
  108. {
  109. if (!in_array((int)$mainId, [52, 1459, 13495], true)) {
  110. return '';
  111. }
  112. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  113. if ($mainId == 1459) {
  114. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  115. }
  116. if ($mainId == 13495) {
  117. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  118. }
  119. return stringUtil::ossBase64($logo);
  120. }
  121. /**
  122. * 花束海报
  123. */
  124. public static function buildMallGoodsPosterUrl($prefix, array $options)
  125. {
  126. return self::buildMallSharePosterUrl($prefix, $options);
  127. }
  128. /**
  129. * 花材海报(商城散客)
  130. */
  131. public static function buildMallItemPosterUrl($prefix, array $options)
  132. {
  133. return self::buildMallSharePosterUrl($prefix, $options);
  134. }
  135. /**
  136. * 花农端花店采购海报(ghs 花材详情太阳码)
  137. */
  138. public static function buildGhsHdItemPosterUrl($prefix, array $options)
  139. {
  140. return self::buildMallSharePosterUrl($prefix, $options);
  141. }
  142. /**
  143. * 散客/花束分享海报:动态画布 + 主图槽位 + 底部信息区
  144. */
  145. private static function buildMallSharePosterUrl($prefix, array $options)
  146. {
  147. $cover = $options['cover'] ?? '';
  148. $miniCode = $options['miniCode'] ?? '';
  149. $priceText = $options['priceText'] ?? '';
  150. $name = $options['name'] ?? '';
  151. $staffText = $options['staffText'] ?? '';
  152. $mainId = (int)($options['mainId'] ?? 0);
  153. $flowerNum = (int)($options['flowerNum'] ?? 0);
  154. $subtitle = trim((string)($options['subtitle'] ?? ''));
  155. $scanHint = trim((string)($options['scanHint'] ?? self::SCAN_HINT));
  156. if ($scanHint === '微信扫码采购') {
  157. $scanHint = self::SCAN_HINT;
  158. }
  159. $coverW = (int)($options['coverWidth'] ?? 0);
  160. $coverH = (int)($options['coverHeight'] ?? 0);
  161. if ($coverW <= 0 || $coverH <= 0) {
  162. list($coverW, $coverH) = imgUtil::getOssImageSize($cover, $prefix);
  163. }
  164. $layout = self::calcPosterLayout($coverW, $coverH);
  165. $imgH = $layout['imgH'];
  166. $totalH = $layout['totalH'];
  167. $scaledH = $layout['scaledH'];
  168. $displayName = self::truncateText($name, 14);
  169. if ($flowerNum > 0 && $displayName !== '') {
  170. $displayName .= ' · ' . $flowerNum . '支';
  171. } elseif ($subtitle !== '' && $displayName !== '') {
  172. $displayName .= ' · ' . self::truncateText($subtitle, 8);
  173. }
  174. $coverWatermark = self::buildCoverWatermarkPath($cover, $imgH, $scaledH, $coverW, $coverH);
  175. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_' . self::QR_SIZE . ',w_' . self::QR_SIZE;
  176. $url = self::buildCanvasBaseUrl($prefix, $totalH);
  177. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark) . ',g_north,x_25,y_8';
  178. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark) . ',g_se,x_' . self::QR_PAD_RIGHT . ',y_' . self::QR_PAD_BOTTOM;
  179. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  180. if ($logoB64 !== '') {
  181. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_210,y_48';
  182. }
  183. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText) . ',g_sw,x_24,y_32,color_EE2C2C,type_' . self::FONT . ',size_56';
  184. $url .= '/watermark,text_' . stringUtil::ossBase64($displayName) . ',g_sw,x_24,y_102,color_333333,type_' . self::FONT . ',size_36';
  185. $url .= '/watermark,text_' . stringUtil::ossBase64($staffText) . ',g_sw,x_24,y_148,color_999999,type_' . self::FONT . ',size_26';
  186. $url .= '/watermark,text_' . stringUtil::ossBase64($scanHint)
  187. . ',g_south,x_' . self::qrCenterX() . ',y_' . self::scanHintYFromBottom()
  188. . ',color_888888,type_' . self::FONT . ',size_24';
  189. return $url;
  190. }
  191. }