FileUtils.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * 文件 utils
  5. *
  6. * @version 0.0.1
  7. * @author by huwhois
  8. * @time 20228/10
  9. */
  10. namespace app\utils;
  11. use think\Image;
  12. use think\Exception;
  13. use think\File;
  14. use think\image\Exception as ImageException;
  15. use think\facade\Config;
  16. use think\file\UploadedFile;
  17. class FileUtils
  18. {
  19. /**
  20. * 图片添加水印
  21. * @param File $file 要处理的文件
  22. * @param int $type 水印类型 0 图片水印, 1 文字水印
  23. * @param string $waterimg 图片水印内容
  24. * @param string $watertext 文字水印内容
  25. * @param string $fonttype 水印文字类型
  26. * @param int $fontsize 水印文字大小
  27. * @param string $fontcolor 水印文字颜色
  28. * @return Image 返回图片对象
  29. */
  30. public function waterMark(
  31. Image $image,
  32. int $type = 0,
  33. string $watermark = '',
  34. string $watertext = '',
  35. string $fonttype = '',
  36. int $fontsize = 0,
  37. string $fontcolor = '#ffffff30'
  38. ): Image {
  39. if ($type == 0) {
  40. $watermark = $watermark ?: Config::get('filesystem.water.watermark');
  41. $image->water($watermark);
  42. } else {
  43. $watetext = $watertext ?: Config::get('filesystem.water.watertext');
  44. $fonttype = $fonttype ?: Config::get('filesystem.water.fonttype');
  45. $fontsize = $fontsize ?: (int) Config::get('filesystem.water.fontsize');
  46. $fontcolor = $fontcolor ?: (int) Config::get('filesystem.water.fontcolor');
  47. $image->text($watetext, $fonttype, $fontsize, $fontcolor);
  48. }
  49. return $image;
  50. }
  51. /**
  52. * 生成缩略图
  53. * @param Image $image 要处理的文件
  54. * @param int $width 缩略图宽值, 默认 384px;
  55. * @param int $height 缩略图高值, 默认 224px;
  56. * @param int $type 缩略图裁剪方式, 默认值 1,固定尺寸缩放; 其他: 1,等比例缩放;2,缩放后填充;3,居中裁剪;4,左上角裁剪;5,右下角裁剪
  57. * @param string $t_suffix 缩略图后缀
  58. * @return Image 返回图片对象
  59. */
  60. public function thumbnail(Image $image, string $thumbname, int $width = 384, int $height = 224, int $type = 1)
  61. {
  62. $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
  63. return $image;
  64. }
  65. /**
  66. * 保存远程图片到本地
  67. */
  68. public static function downloadUrlImg(string $url)
  69. {
  70. $ch = curl_init($url);
  71. curl_setopt($ch, CURLOPT_HEADER, 0);
  72. curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  75. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  76. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  77. $package = curl_exec($ch);
  78. $httpinfo = curl_getinfo($ch);
  79. curl_close($ch);
  80. // halt($httpinfo);
  81. $imageAll = array_merge(array(
  82. 'imgBody' => $package
  83. ), $httpinfo);
  84. if ($httpinfo['download_content_length'] > 4 * 1024 * 1024) {
  85. throw new Exception("文件太大", 1);
  86. }
  87. $type = null;
  88. switch ($imageAll['content_type']) {
  89. case 'image/gif':
  90. $type = "gif";
  91. break;
  92. case 'image/webp':
  93. $type = "webp";
  94. break;
  95. case 'image/jpeg':
  96. $type = "jpg";
  97. break;
  98. case 'image/png':
  99. $type = "png";
  100. break;
  101. default:
  102. $type = null;
  103. break;
  104. }
  105. // 腾讯公众号图片
  106. if (strpos($url, 'qpic.cn') !== false) {
  107. $urls = parse_url($url);
  108. if (isset($urls['query'])) {
  109. $query_arr = [];
  110. parse_str($urls['query'], $query_arr);
  111. $type = isset($query_arr['wx_fmt']) ? $query_arr['wx_fmt'] : null;
  112. $type = $type == 'jpeg' ? 'jpg' : $type;
  113. }
  114. }
  115. if (!$type) {
  116. throw new Exception("不支持的文件后缀", 1);
  117. }
  118. $rootpath = Config::get('filesystem.disks.public.root');
  119. $filepath = date("Ymd");
  120. if (!file_exists($rootpath . "/" . $filepath)) {
  121. mkdir($rootpath . "/" . $filepath, 0755);
  122. }
  123. $savename = $filepath . "/" . md5(microtime(true). substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 6)). "." . $type;
  124. $filename = Config::get('filesystem.disks.public.url') . '/' . $savename;
  125. file_put_contents($rootpath . "/" . $savename, $imageAll["imgBody"]);
  126. return $filename;
  127. }
  128. /**
  129. * 遍历获取目录下的指定类型的文件
  130. * @param $path
  131. * @param $allowFiles png|jpg|jpeg|gif|bmp|webp
  132. * @param array $files
  133. * @return array
  134. */
  135. public static function getFiles($path, $allowFiles = 'png|jpg|jpeg|gif|bmp|webp', &$files = array())
  136. {
  137. if (!is_dir($path)) return null;
  138. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  139. $handle = opendir($path);
  140. while (false !== ($file = readdir($handle))) {
  141. if ($file != '.' && $file != '..') {
  142. $path2 = $path . $file;
  143. if (is_dir($path2)) {
  144. self::getFiles($path2, $allowFiles, $files);
  145. } else {
  146. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  147. $files[] = array(
  148. 'url' => substr($path2, strlen(app()->getRootPath() . '/public') - 1),
  149. 'mtime' => filemtime($path2)
  150. );
  151. }
  152. }
  153. }
  154. }
  155. return $files;
  156. }
  157. }