UploadService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/10/24
  6. */
  7. namespace service;
  8. class UploadService
  9. {
  10. private static $uploadStatus;
  11. //上传图片的大小 2MB 单位字节
  12. private static $imageValidate = ['size'=>2097152,'ext'=>'jpg,jpeg,png,gif','mime'=>'image/jpeg,image/gif,image/png'];
  13. /**
  14. * 初始化
  15. */
  16. private static function init()
  17. {
  18. self::$uploadStatus = new \StdClass();
  19. }
  20. /**
  21. * 返回失败信息
  22. * @param $error
  23. * @return mixed
  24. */
  25. protected static function setError($error)
  26. {
  27. self::$uploadStatus->status = false;
  28. self::$uploadStatus->error = $error;
  29. return self::$uploadStatus;
  30. }
  31. /**
  32. * 返回成功信息
  33. * @param $path
  34. * @param \think\File $fileInfo
  35. * @return mixed
  36. */
  37. protected static function successful($path, \think\File $fileInfo)
  38. {
  39. $filePath = DS . $path . DS . $fileInfo->getSaveName();
  40. self::$uploadStatus->filePath = self::pathToUrl($filePath);
  41. self::$uploadStatus->fileInfo = $fileInfo;
  42. self::$uploadStatus->uploadPath = $path;
  43. self::$uploadStatus->dir = $filePath;
  44. self::$uploadStatus->status = true;
  45. return self::$uploadStatus;
  46. }
  47. /**
  48. * 检查上传目录不存在则生成
  49. * @param $dir
  50. * @return bool
  51. */
  52. protected static function validDir($dir)
  53. {
  54. return is_dir($dir) == true || mkdir($dir,0777,true) == true;
  55. }
  56. /**
  57. * 开启/关闭上出文件验证
  58. * @param bool $bool
  59. */
  60. protected static function autoValidate($bool = false)
  61. {
  62. self::$autoValidate = $bool;
  63. }
  64. /**
  65. * 生成上传文件目录
  66. * @param $path
  67. * @param null $root
  68. * @return string
  69. */
  70. protected static function uploadDir($path, $root=null)
  71. {
  72. if($root === null) $root = 'public/' . 'uploads';
  73. return $root . DS . $path;
  74. }
  75. /**
  76. * 单图上传
  77. * @param string $fileName 上传文件名
  78. * @param string $path 上传路径
  79. * @param bool $moveName 生成文件名
  80. * @param bool $autoValidate 是否开启文件验证
  81. * @param null $root 上传根目录路径
  82. * @param string $rule 文件名自动生成规则
  83. * @return mixed
  84. */
  85. public static function image($fileName, $path, $moveName = true, $autoValidate=true, $root=null, $rule='uniqid')
  86. {
  87. self::init();
  88. $path = self::uploadDir($path,$root);
  89. $dir = ROOT_PATH . $path;
  90. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  91. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  92. $file = request()->file($fileName);
  93. if($autoValidate) $file = $file->validate(self::$imageValidate);
  94. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  95. if(false === $fileInfo) return self::setError($file->getError());
  96. return self::successful($path,$fileInfo);
  97. }
  98. /**
  99. * 文件上传
  100. * @param string $fileName 上传文件名
  101. * @param string $path 上传路径
  102. * @param bool $moveName 生成文件名
  103. * @param bool $autoValidate 验证规则 [size:1024,ext:[],type:[]]
  104. * @param null $root 上传根目录路径
  105. * @param string $rule 文件名自动生成规则
  106. * @return mixed
  107. */
  108. public static function file($fileName, $path, $moveName = true, $autoValidate=[], $root=null, $rule='uniqid')
  109. {
  110. self::init();
  111. $path = self::uploadDir($path,$root);
  112. $dir = ROOT_PATH . $path;
  113. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  114. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  115. $extension = strtolower(pathinfo($_FILES[$fileName]['name'], PATHINFO_EXTENSION));
  116. if(strtolower($extension) == 'php' || !$extension)
  117. return self::setError('上传文件非法!');
  118. $file = request()->file($fileName);
  119. if(count($autoValidate)>0) $file = $file->validate($autoValidate);
  120. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  121. if(false === $fileInfo) return self::setError($file->getError());
  122. return self::successful($path,$fileInfo);
  123. }
  124. public static function pathToUrl($path)
  125. {
  126. return trim(str_replace(DS, '/', $path),'.');
  127. }
  128. public static function openImage($filePath)
  129. {
  130. return \think\Image::open($filePath);
  131. }
  132. /**
  133. * 图片压缩
  134. *
  135. * @param string $filePath 文件路径
  136. * @param int $ratio 缩放比例 1-9
  137. * @param string $pre 前缀
  138. * @return string 压缩图片路径
  139. */
  140. public static function thumb($filePath, $ratio=8, $pre='s_')
  141. {
  142. $filePath = '.'.ltrim($filePath,'.');
  143. $img = self::openImage($filePath);
  144. $width = $img->width() * $ratio / 10;
  145. $height = $img->height() * $ratio / 10;
  146. $dir = dirname($filePath);
  147. $fileName = basename($filePath);
  148. $savePath = $dir.DS.$pre.$fileName;
  149. $img->thumb($width,$height)->save($savePath);
  150. return ltrim($savePath,'.');
  151. }
  152. }