ImgHelper.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/8/10
  6. * Time: 17:43
  7. */
  8. namespace file;
  9. use think\Image;
  10. class ImgHelper
  11. {
  12. /**
  13. * 判断该文件或路径是否是一个图片文件
  14. * https://www.jb51.net/article/102260.htm
  15. * @param $filepath
  16. * @return bool
  17. */
  18. public static function isImage($filepath)
  19. {
  20. //如果本地有文件,通过文件信息判断
  21. if (function_exists('exif_imagetype') && file_exists($filepath)) {
  22. $mimetype = @exif_imagetype($filepath);
  23. if ($mimetype == IMAGETYPE_GIF || $mimetype == IMAGETYPE_JPEG || $mimetype == IMAGETYPE_PNG || $mimetype == IMAGETYPE_BMP) {
  24. return true;
  25. }
  26. return false;
  27. }
  28. //否则通过文件后缀判断
  29. $ext = strtolower(PathHelper::getExt($filepath));
  30. if (in_array($ext, ['gif', 'jpg', 'jpeg', 'png', 'bmp'])) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. /**
  36. * 缩略图
  37. * @param $filePath [原图片物理路径]
  38. * @param string $savePath [保存路径]
  39. * @param int $width [缩略图最大宽度]
  40. * @param int $height [缩略图最大高度]
  41. * @param int $type [缩略图裁剪类型]
  42. * @return $obj /think/Image
  43. */
  44. function thumb($filePath, $width, $height, $savePath = '', $type = 2)
  45. {
  46. if (!$savePath) {
  47. $savePath = $filePath;//覆盖原图
  48. }
  49. $image = Image::open($filePath);
  50. return $image->thumb($width, $height, $type)->save($savePath);//生成缩略图
  51. }
  52. }