FileService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <<<<<<< HEAD
  2. <?php
  3. declare(strict_types=1);
  4. /**
  5. * 文件 service
  6. *
  7. * @version 0.0.1
  8. * @author by huwhois
  9. * @time 2021/12/28
  10. */
  11. namespace app\common\service;
  12. use think\Image;
  13. use think\Exception;
  14. use think\File;
  15. use think\image\Exception as ImageException;
  16. use think\facade\Config;
  17. class FileService
  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 static function waterMark(File $file, int $type = 0, string $watermark = '', string $watertext = '',
  31. string $fonttype = '', int $fontsize = 0, string $fontcolor = '#ffffff30'): Image
  32. {
  33. $image = Image::open($file);
  34. if ($type == 0) {
  35. $watermark = $watermark ?: Config::get('filesystem.water.watermark');
  36. $image->water($watermark);
  37. } else {
  38. $watetext = $watertext ?: Config::get('filesystem.water.watertext');
  39. $fonttype = $fonttype ?: Config::get('filesystem.water.fonttype');
  40. $fontsize = $fontsize ?: (int) Config::get('filesystem.water.fontsize');
  41. $fontcolor = $fontcolor ?: (int) Config::get('filesystem.water.fontcolor');
  42. $image->text($watetext, $fonttype, $fontsize, $fontcolor);
  43. }
  44. return $image;
  45. }
  46. /**
  47. * 生成缩略图
  48. * @param File $file 要处理的文件
  49. * @param int $width 缩略图宽值, 默认 384px;
  50. * @param int $height 缩略图高值, 默认 224px;
  51. * @param int $type 缩略图裁剪方式, 默认值 1,固定尺寸缩放; 其他: 1,等比例缩放;2,缩放后填充;3,居中裁剪;4,左上角裁剪;5,右下角裁剪
  52. * @param string $t_suffix 缩略图后缀
  53. * @return Image 返回图片对象
  54. */
  55. public static function thumbnail(File $file, int $width = 384, int $height = 224, int $type = 1, string $t_suffix = "thumb")
  56. {
  57. $image = Image::open($file);
  58. $ext = $file->getExtension();
  59. $filename = $file->getFilename();
  60. $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $t_suffix . '.' . $ext;
  61. $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
  62. return $image;
  63. }
  64. /**
  65. * 保存远程图片到本地
  66. */
  67. public function downloadUrlImg(string $url)
  68. {
  69. $ch = curl_init($url);
  70. curl_setopt($ch, CURLOPT_HEADER, 0);
  71. curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头
  72. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  74. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  75. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  76. $package = curl_exec($ch);
  77. $httpinfo = curl_getinfo($ch);
  78. curl_close($ch);
  79. $imageAll = array_merge(array(
  80. 'imgBody' => $package
  81. ), $httpinfo);
  82. if ($httpinfo['download_content_length'] > 4 * 1024 * 1024) {
  83. throw new Exception("文件太大", 1);
  84. }
  85. $type = null;
  86. switch ($imageAll['content_type']) {
  87. case 'image/gif':
  88. $type = "gif";
  89. break;
  90. case 'image/webp':
  91. $type = "webp";
  92. break;
  93. case 'image/jpeg':
  94. $type = "jpg";
  95. break;
  96. case 'image/png':
  97. $type = "png";
  98. break;
  99. default:
  100. $type = null;
  101. break;
  102. }
  103. // 腾讯公众号图片
  104. if(strpos($url,'qpic.cn') !== false){
  105. $urls = parse_url($url);
  106. if (isset($urls['query'])) {
  107. $query_arr = [];
  108. parse_str($urls['query'], $query_arr);
  109. $type = isset($query_arr['wx_fmt']) ? $query_arr['wx_fmt'] : null;
  110. $type = $type == 'jpeg' ? 'jpg' : $type;
  111. }
  112. }
  113. if (!$type) {
  114. throw new Exception("不支持的文件后缀", 1);
  115. }
  116. $temp = app()->getRuntimePath() . 'temp';
  117. if (!file_exists($temp)) {
  118. mkdir($temp, 0755);
  119. }
  120. $tempname = $temp . '/php.' . $type;
  121. file_put_contents($tempname, $imageAll["imgBody"]);
  122. return new File($tempname);
  123. }
  124. /**
  125. * 遍历获取目录下的指定类型的文件
  126. * @param $path
  127. * @param $allowFiles png|jpg|jpeg|gif|bmp|webp
  128. * @param array $files
  129. * @return array
  130. */
  131. public static function getFiles($path, $allowFiles = 'png|jpg|jpeg|gif|bmp|webp', &$files = array())
  132. {
  133. if (!is_dir($path)) return null;
  134. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  135. $handle = opendir($path);
  136. while (false !== ($file = readdir($handle))) {
  137. if ($file != '.' && $file != '..') {
  138. $path2 = $path . $file;
  139. if (is_dir($path2)) {
  140. self::getFiles($path2, $allowFiles, $files);
  141. } else {
  142. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  143. $files[] = array(
  144. 'url' => substr($path2, strlen(app()->getRootPath().'/public') - 1),
  145. 'mtime' => filemtime($path2)
  146. );
  147. }
  148. }
  149. }
  150. }
  151. return $files;
  152. }
  153. }
  154. =======
  155. <?php
  156. declare(strict_types=1);
  157. /**
  158. * 文件 service
  159. *
  160. * @version 0.0.1
  161. * @author by huwhois
  162. * @time 2021/12/28
  163. */
  164. namespace app\common\service;
  165. use think\Image;
  166. use think\Exception;
  167. use think\File;
  168. use think\image\Exception as ImageException;
  169. use think\facade\Config;
  170. class FileService
  171. {
  172. /**
  173. * 图片添加水印
  174. * @param File $file 要处理的文件
  175. * @param int $type 水印类型 0 图片水印, 1 文字水印
  176. * @param string $waterimg 图片水印内容
  177. * @param string $watertext 文字水印内容
  178. * @param string $fonttype 水印文字类型
  179. * @param int $fontsize 水印文字大小
  180. * @param string $fontcolor 水印文字颜色
  181. * @return Image 返回图片对象
  182. */
  183. public function waterMark(Image $image, int $type = 0, string $watermark = '', string $watertext = '',
  184. string $fonttype = '', int $fontsize = 0, string $fontcolor = '#ffffff30'): Image
  185. {
  186. if ($type == 0) {
  187. $watermark = $watermark ?: Config::get('filesystem.water.watermark');
  188. $image->water($watermark);
  189. } else {
  190. $watetext = $watertext ?: Config::get('filesystem.water.watertext');
  191. $fonttype = $fonttype ?: Config::get('filesystem.water.fonttype');
  192. $fontsize = $fontsize ?: (int) Config::get('filesystem.water.fontsize');
  193. $fontcolor = $fontcolor ?: (int) Config::get('filesystem.water.fontcolor');
  194. $image->text($watetext, $fonttype, $fontsize, $fontcolor);
  195. }
  196. return $image;
  197. }
  198. /**
  199. * 生成缩略图
  200. * @param Image $image 要处理的文件
  201. * @param int $width 缩略图宽值, 默认 384px;
  202. * @param int $height 缩略图高值, 默认 224px;
  203. * @param int $type 缩略图裁剪方式, 默认值 1,固定尺寸缩放; 其他: 1,等比例缩放;2,缩放后填充;3,居中裁剪;4,左上角裁剪;5,右下角裁剪
  204. * @param string $t_suffix 缩略图后缀
  205. * @return Image 返回图片对象
  206. */
  207. public function thumbnail(Image $image, string $thumbname, int $width = 384, int $height = 224, int $type = 1)
  208. {
  209. $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
  210. return $image;
  211. }
  212. /**
  213. * 保存远程图片到本地
  214. */
  215. public function downloadUrlImg(string $url)
  216. {
  217. $ch = curl_init($url);
  218. curl_setopt($ch, CURLOPT_HEADER, 0);
  219. curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头
  220. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  221. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  222. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  223. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  224. $package = curl_exec($ch);
  225. $httpinfo = curl_getinfo($ch);
  226. curl_close($ch);
  227. $imageAll = array_merge(array(
  228. 'imgBody' => $package
  229. ), $httpinfo);
  230. if ($httpinfo['download_content_length'] > 4 * 1024 * 1024) {
  231. throw new Exception("文件太大", 1);
  232. }
  233. $type = null;
  234. switch ($imageAll['content_type']) {
  235. case 'image/gif':
  236. $type = "gif";
  237. break;
  238. case 'image/webp':
  239. $type = "webp";
  240. break;
  241. case 'image/jpeg':
  242. $type = "jpg";
  243. break;
  244. case 'image/png':
  245. $type = "png";
  246. break;
  247. default:
  248. $type = null;
  249. break;
  250. }
  251. // 腾讯公众号图片
  252. if(strpos($url,'qpic.cn') !== false){
  253. $urls = parse_url($url);
  254. if (isset($urls['query'])) {
  255. $query_arr = [];
  256. parse_str($urls['query'], $query_arr);
  257. $type = isset($query_arr['wx_fmt']) ? $query_arr['wx_fmt'] : null;
  258. $type = $type == 'jpeg' ? 'jpg' : $type;
  259. }
  260. }
  261. if (!$type) {
  262. throw new Exception("不支持的文件后缀", 1);
  263. }
  264. $temp = app()->getRuntimePath() . 'temp';
  265. if (!file_exists($temp)) {
  266. mkdir($temp, 0755);
  267. }
  268. $tempname = $temp . '/php.' . $type;
  269. file_put_contents($tempname, $imageAll["imgBody"]);
  270. return new File($tempname);
  271. }
  272. /**
  273. * 遍历获取目录下的指定类型的文件
  274. * @param $path
  275. * @param $allowFiles png|jpg|jpeg|gif|bmp|webp
  276. * @param array $files
  277. * @return array
  278. */
  279. public function getFiles($path, $allowFiles = 'png|jpg|jpeg|gif|bmp|webp', &$files = array())
  280. {
  281. if (!is_dir($path)) return null;
  282. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  283. $handle = opendir($path);
  284. while (false !== ($file = readdir($handle))) {
  285. if ($file != '.' && $file != '..') {
  286. $path2 = $path . $file;
  287. if (is_dir($path2)) {
  288. self::getFiles($path2, $allowFiles, $files);
  289. } else {
  290. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  291. $files[] = array(
  292. 'url' => substr($path2, strlen(app()->getRootPath().'/public') - 1),
  293. 'mtime' => filemtime($path2)
  294. );
  295. }
  296. }
  297. }
  298. }
  299. return $files;
  300. }
  301. }
  302. >>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86