CommonHelper.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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,$boxSize=100,$imgSize=350)
  11. {
  12. if (empty($images) || empty($images[0])) {
  13. $html = "";
  14. } else {
  15. //默认用等比例缩放
  16. $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
  17. $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
  18. foreach ($images as $image) {
  19. $html .= "<div style='width: {$boxSize}px; height: {$boxSize}px; padding: 3px; border: 1px solid #ddd; border-radius: 3px; background-color: #f9f9f9; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
  20. <img data-action='preview-img' src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
  21. </div>";
  22. }
  23. $html .= '</div>';
  24. }
  25. return $html;
  26. }
  27. /*
  28. * 返回oss的url
  29. */
  30. public static function ossUrl($image)
  31. {
  32. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
  33. }
  34. }