FileService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * 文件 service
  5. *
  6. * @version 0.0.1
  7. * @author by huwhois
  8. * @time 2021/12/28
  9. */
  10. namespace app\common\service;
  11. use think\Image;
  12. use think\facade\Filesystem;
  13. use think\Exception;
  14. use think\File;
  15. class FileService
  16. {
  17. /**
  18. * 生成缩略图
  19. * @param string $filename 必须参数, 图片路径名(相对or绝对)(/public/uploads/...)
  20. * @param int $width 缩略图宽值, 默认 384px;
  21. * @param int $height 缩略图高值, 默认 224px;
  22. * @param int $type 缩略图裁剪方式, 默认值 1, 固定尺寸缩放; 其他: 1, 等比例缩放;
  23. * 2, 缩放后填充; 3, 居中裁剪; 4, 左上角裁剪; 5, 右下角裁剪
  24. * @return string $thumbname 缩略图文件名
  25. */
  26. public static function makeThumb($filename, $width = 384, $height = 224, $type = 1, $t_suffix = "thumb")
  27. {
  28. $file = './storage/' . str_replace('\\', '/', $filename);
  29. $ext = pathinfo($file, PATHINFO_EXTENSION);
  30. $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $t_suffix . '.' . $ext;
  31. $image = Image::open($file);
  32. $result = $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
  33. return $result ? $thumbname : '';
  34. }
  35. public function urlImg($url)
  36. {
  37. $ch = curl_init($url);
  38. curl_setopt($ch, CURLOPT_HEADER, 0);
  39. curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. $package = curl_exec($ch);
  42. $httpinfo = curl_getinfo($ch);
  43. curl_close($ch);
  44. $imageAll = array_merge(array(
  45. 'imgBody' => $package
  46. ), $httpinfo);
  47. if ($httpinfo['download_content_length'] > 4 * 1024 * 1024) {
  48. throw new Exception("文件太大", 1);
  49. }
  50. $type = null;
  51. switch ($imageAll['content_type']) {
  52. case 'image/gif':
  53. $type = "gif";
  54. break;
  55. case 'image/webp':
  56. $type = "webp";
  57. break;
  58. case 'image/jpeg':
  59. $type = "jpg";
  60. break;
  61. case 'image/png':
  62. $type = "png";
  63. break;
  64. default:
  65. $type = null;
  66. break;
  67. }
  68. if (!$type) {
  69. throw new Exception("不支持的文件后缀", 1);
  70. }
  71. $temp = app()->getRuntimePath() . 'temp';
  72. if (!file_exists($temp)) {
  73. mkdir($temp, 0755);
  74. }
  75. $tempname = $temp . '/php.' . $type;
  76. file_put_contents($tempname, $imageAll["imgBody"]);
  77. return new File($tempname);
  78. }
  79. /**
  80. * 遍历获取目录下的指定类型的文件
  81. * @param $path
  82. * @param $allowFiles png|jpg|jpeg|gif|bmp|webp
  83. * @param array $files
  84. * @return array
  85. */
  86. public static function getFiles($path, $allowFiles = 'png|jpg|jpeg|gif|bmp|webp', &$files = array())
  87. {
  88. if (!is_dir($path)) return null;
  89. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  90. $handle = opendir($path);
  91. while (false !== ($file = readdir($handle))) {
  92. if ($file != '.' && $file != '..') {
  93. $path2 = $path . $file;
  94. if (is_dir($path2)) {
  95. self::getFiles($path2, $allowFiles, $files);
  96. } else {
  97. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  98. $files[] = array(
  99. 'url' => substr($path2, strlen(app()->getRootPath().'/public') - 1),
  100. 'mtime' => filemtime($path2)
  101. );
  102. }
  103. }
  104. }
  105. }
  106. return $files;
  107. }
  108. }