FileService.php 5.7 KB

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