ImageHelper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\helpers\Json;
  6. /**
  7. * Class ImageHelper
  8. * @package common\helpers
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class ImageHelper
  12. {
  13. /**
  14. * 默认图片
  15. *
  16. * @param $imgSrc
  17. * @param string $defaultImgSre
  18. * @return string
  19. */
  20. public static function default($imgSrc, $defaultImgSre = '@baseResources/img/img-error.png')
  21. {
  22. return !empty($imgSrc) ? Yii::getAlias($imgSrc) : Yii::getAlias($defaultImgSre);
  23. }
  24. /**
  25. * 默认头像
  26. *
  27. * @param $imgSrc
  28. */
  29. public static function defaultHeaderPortrait($imgSrc, $defaultImgSre = '@baseResources/img/profile_small.jpg')
  30. {
  31. return !empty($imgSrc) ? Yii::getAlias($imgSrc) : Yii::getAlias($defaultImgSre);
  32. }
  33. /**
  34. * 点击大图
  35. *
  36. * @param string $imgSrc
  37. * @param int $width 宽度 默认45px
  38. * @param int $height 高度 默认45px
  39. */
  40. public static function fancyBox($imgSrc, $width = 40, $height = 40)
  41. {
  42. $image = Html::img($imgSrc, [
  43. 'width' => $width,
  44. 'height' => $height,
  45. ]);
  46. return Html::a($image, $imgSrc, [
  47. 'data-fancybox' => 'gallery'
  48. ]);
  49. }
  50. /**
  51. * 显示图片列表
  52. *
  53. * @param $covers
  54. * @return string
  55. */
  56. public static function fancyBoxs($covers, $width = 45, $height = 45)
  57. {
  58. $image = '';
  59. if (empty($covers)) {
  60. return $image;
  61. }
  62. !is_array($covers) && $covers = Json::decode($covers);
  63. foreach ($covers as $cover) {
  64. $image .= Html::tag('span', self::fancyBox($cover, $width, $height), [
  65. 'style' => 'padding-right:5px;padding-bottom:5px'
  66. ]);
  67. }
  68. return $image;
  69. }
  70. /**
  71. * 判断是否图片地址
  72. *
  73. * @param string $imgSrc
  74. * @return bool
  75. */
  76. public static function isImg($imgSrc)
  77. {
  78. $extend = StringHelper::clipping($imgSrc, '.', 1);
  79. $imgExtends = [
  80. 'bmp',
  81. 'jpg',
  82. 'gif',
  83. 'jpeg',
  84. 'jpe',
  85. 'jpg',
  86. 'png',
  87. 'jif',
  88. 'dib',
  89. 'rle',
  90. 'emf',
  91. 'pcx',
  92. 'dcx',
  93. 'pic',
  94. 'tga',
  95. 'tif',
  96. 'tiffxif',
  97. 'wmf',
  98. 'jfif'
  99. ];
  100. if (in_array($extend, $imgExtends) || strpos($imgSrc, 'http://wx.qlogo.cn') !== false) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. }
粤ICP备19079148号