CommonHelper.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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=60,$imgSize=1024)
  11. {
  12. if (empty($images) || empty($images[0])) {
  13. $html = "";
  14. } else {
  15. // 默认显示 100x100 的图片
  16. $thumbnailImages = array_map(function ($imageUrl) use ($boxSize) {
  17. $imageUrl= CommonHelper::ossUrl($imageUrl);
  18. return $imageUrl . "?x-oss-process=image/resize,m_lfit,h_{$boxSize}";
  19. }, $images);
  20. // 生成点击查看大图的链接
  21. $largeImages = array_map(function ($imageUrl) use ($imgSize) {
  22. $imageUrl= CommonHelper::ossUrl($imageUrl);
  23. return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
  24. }, $images);
  25. // 显示缩略图,并添加点击查看大图的功能
  26. $html = '';
  27. foreach ($thumbnailImages as $index => $thumbnailUrl) {
  28. $largeUrl = $largeImages[$index];
  29. $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px;'></a>";
  30. }
  31. return $html;
  32. //
  33. // //默认用等比例缩放
  34. // $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
  35. // $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
  36. // foreach ($images as $image) {
  37. // $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;'>
  38. // <img data-action='preview-img' src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
  39. // </div>";
  40. // }
  41. // $html .= '</div>';
  42. }
  43. return $html;
  44. }
  45. /*
  46. * 返回oss的url
  47. */
  48. public static function ossUrl($image)
  49. {
  50. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
  51. }
  52. }