FileService.php 4.3 KB

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