PosterUtil.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace common\components;
  3. /**
  4. * 散客分享海报(OSS 图片水印合成)
  5. */
  6. class PosterUtil
  7. {
  8. const BASE_TEMPLATE = 'poster/1234.jpg';
  9. const FONT = 'd3F5LW1pY3JvaGVp';
  10. /**
  11. * 海报标题过长时截断,避免压住底部文案区
  12. */
  13. public static function truncateText($text, $maxLen = 14)
  14. {
  15. $text = trim((string)$text);
  16. if ($text === '') {
  17. return '';
  18. }
  19. if (mb_strlen($text, 'UTF-8') > $maxLen) {
  20. return mb_substr($text, 0, $maxLen, 'UTF-8') . '…';
  21. }
  22. return $text;
  23. }
  24. /**
  25. * 和炫鲜花等门店角标
  26. */
  27. public static function buildHyxhLogoWatermark($mainId)
  28. {
  29. if (!in_array((int)$mainId, [52, 1459, 13495], true)) {
  30. return '';
  31. }
  32. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  33. if ($mainId == 1459) {
  34. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  35. }
  36. if ($mainId == 13495) {
  37. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_72,w_72';
  38. }
  39. return stringUtil::ossBase64($logo);
  40. }
  41. /**
  42. * 花束海报:主图 m_lfit 保留完整花束,底部价格/标题/扫码区层次更清晰
  43. */
  44. public static function buildMallGoodsPosterUrl($prefix, array $options)
  45. {
  46. $cover = $options['cover'] ?? '';
  47. $miniCode = $options['miniCode'] ?? '';
  48. $priceText = $options['priceText'] ?? '';
  49. $name = $options['name'] ?? '';
  50. $staffText = $options['staffText'] ?? '';
  51. $mainId = (int)($options['mainId'] ?? 0);
  52. $flowerNum = (int)($options['flowerNum'] ?? 0);
  53. $displayName = self::truncateText($name, 14);
  54. if ($flowerNum > 0 && $displayName !== '') {
  55. $displayName .= ' · ' . $flowerNum . '支';
  56. }
  57. $coverWatermark = $cover . '?x-oss-process=image/resize,m_lfit,h_1020,w_700,limit_0';
  58. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_220,w_220';
  59. $url = $prefix . self::BASE_TEMPLATE . '?x-oss-process=image';
  60. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark) . ',g_north,x_25,y_8';
  61. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark) . ',g_se,x_36,y_32';
  62. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  63. if ($logoB64 !== '') {
  64. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_210,y_48';
  65. }
  66. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText) . ',g_sw,x_24,y_32,color_EE2C2C,type_' . self::FONT . ',size_56';
  67. $url .= '/watermark,text_' . stringUtil::ossBase64($displayName) . ',g_sw,x_24,y_102,color_333333,type_' . self::FONT . ',size_36';
  68. $url .= '/watermark,text_' . stringUtil::ossBase64($staffText) . ',g_sw,x_24,y_148,color_999999,type_' . self::FONT . ',size_26';
  69. $url .= '/watermark,text_' . stringUtil::ossBase64('微信扫码下单') . ',g_se,x_44,y_292,color_888888,type_' . self::FONT . ',size_24';
  70. return $url;
  71. }
  72. /**
  73. * 花材海报:方图裁切,保持原有布局
  74. */
  75. public static function buildMallItemPosterUrl($prefix, array $options)
  76. {
  77. $cover = $options['cover'] ?? '';
  78. $miniCode = $options['miniCode'] ?? '';
  79. $priceText = $options['priceText'] ?? '';
  80. $name = $options['name'] ?? '';
  81. $staffText = $options['staffText'] ?? '';
  82. $mainId = (int)($options['mainId'] ?? 0);
  83. $coverWatermark = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  84. $qrWatermark = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  85. $url = $prefix . self::BASE_TEMPLATE . '?x-oss-process=image';
  86. $url .= '/watermark,image_' . stringUtil::ossBase64($coverWatermark) . ',g_nw,x_0,y_0';
  87. $url .= '/watermark,image_' . stringUtil::ossBase64($qrWatermark) . ',g_se';
  88. $logoB64 = self::buildHyxhLogoWatermark($mainId);
  89. if ($logoB64 !== '') {
  90. $url .= '/watermark,image_' . $logoB64 . ',g_se,x_65,y_65';
  91. }
  92. $url .= '/watermark,text_' . stringUtil::ossBase64($staffText) . ',g_sw,x_18,y_235,color_8B8989,type_' . self::FONT . ',size_30';
  93. $url .= '/watermark,text_' . stringUtil::ossBase64(self::truncateText($name, 16)) . ',g_sw,x_18,y_145';
  94. $url .= '/watermark,text_' . stringUtil::ossBase64($priceText) . ',g_sw,x_18,y_50,color_EE2C2C,type_' . self::FONT . ',size_50';
  95. return $url;
  96. }
  97. }