FileService.php 5.4 KB

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