CommonHelper.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. // app/Libraries/CommonHelper.php
  3. namespace App\Libraries;
  4. class CommonHelper
  5. {
  6. /*
  7. * $images 格式:['image.jpg','image2.jpg']
  8. * 返回显示的HTML显示图片
  9. */
  10. public static function displayImage($images,$size=150)
  11. {
  12. //默认用等比例缩放
  13. $process = "?x-oss-process=image/resize,h_{$size},m_lfit";
  14. $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
  15. foreach ($images as $image) {
  16. $html .= "<div style='flex: 1 0 {$size}px; max-width: {$size}px; max-height: {$size}px; padding: 5px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
  17. <img data-action='preview-img' src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
  18. </div>";
  19. }
  20. $html .= '</div>';
  21. return $html;
  22. }
  23. /*
  24. * 返回oss的url
  25. */
  26. public static function ossUrl($image)
  27. {
  28. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
  29. }
  30. }