UploadService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if(strpos(PUBILC_PATH,'public') !== false){
  44. self::$uploadStatus->dir = $filePath;
  45. }else{
  46. self::$uploadStatus->dir = str_replace('/public','',$filePath);
  47. }
  48. self::$uploadStatus->status = true;
  49. return self::$uploadStatus;
  50. }
  51. /**
  52. * 检查上传目录不存在则生成
  53. * @param $dir
  54. * @return bool
  55. */
  56. protected static function validDir($dir)
  57. {
  58. return is_dir($dir) == true || mkdir($dir,0777,true) == true;
  59. }
  60. /**
  61. * 开启/关闭上出文件验证
  62. * @param bool $bool
  63. */
  64. protected static function autoValidate($bool = false)
  65. {
  66. self::$autoValidate = $bool;
  67. }
  68. /**
  69. * 生成上传文件目录
  70. * @param $path
  71. * @param null $root
  72. * @return string
  73. */
  74. protected static function uploadDir($path, $root=null)
  75. {
  76. if($root === null) $root = UPLOAD_PATH;
  77. return $root . DS . $path;
  78. }
  79. /**
  80. * 单图上传
  81. * @param string $fileName 上传文件名
  82. * @param string $path 上传路径
  83. * @param bool $moveName 生成文件名
  84. * @param bool $autoValidate 是否开启文件验证
  85. * @param null $root 上传根目录路径
  86. * @param string $rule 文件名自动生成规则
  87. * @return mixed
  88. */
  89. public static function image($fileName, $path, $moveName = true, $autoValidate=true, $root=null, $rule='uniqid')
  90. {
  91. self::init();
  92. $path = self::uploadDir($path,$root);
  93. $dir = ROOT_PATH . $path;
  94. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  95. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  96. $file = request()->file($fileName);
  97. if($autoValidate) $file = $file->validate(self::$imageValidate);
  98. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  99. if(false === $fileInfo) return self::setError($file->getError());
  100. return self::successful($path,$fileInfo);
  101. }
  102. /**
  103. * 文件上传
  104. * @param string $fileName 上传文件名
  105. * @param string $path 上传路径
  106. * @param bool $moveName 生成文件名
  107. * @param bool $autoValidate 验证规则 [size:1024,ext:[],type:[]]
  108. * @param null $root 上传根目录路径
  109. * @param string $rule 文件名自动生成规则
  110. * @return mixed
  111. */
  112. public static function file($fileName, $path, $moveName = true, $autoValidate=[], $root=null, $rule='uniqid')
  113. {
  114. self::init();
  115. $path = self::uploadDir($path,$root);
  116. $dir = ROOT_PATH . $path;
  117. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  118. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  119. $extension = strtolower(pathinfo($_FILES[$fileName]['name'], PATHINFO_EXTENSION));
  120. if(strtolower($extension) == 'php' || !$extension)
  121. return self::setError('上传文件非法!');
  122. $file = request()->file($fileName);
  123. if(count($autoValidate)>0) $file = $file->validate($autoValidate);
  124. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  125. if(false === $fileInfo) return self::setError($file->getError());
  126. return self::successful($path,$fileInfo);
  127. }
  128. public static function pathToUrl($path)
  129. {
  130. return trim(str_replace(DS, '/', $path),'.');
  131. }
  132. public static function openImage($filePath)
  133. {
  134. return \think\Image::open($filePath);
  135. }
  136. /**
  137. * 图片压缩
  138. *
  139. * @param string $filePath 文件路径
  140. * @param int $ratio 缩放比例 1-9
  141. * @param string $pre 前缀
  142. * @return string 压缩图片路径
  143. */
  144. public static function thumb($filePath, $ratio=8, $pre='s_')
  145. {
  146. $filePath = ltrim($filePath,'/');
  147. $img = self::openImage($filePath);
  148. $width = $img->width() * $ratio / 10;
  149. $height = $img->height() * $ratio / 10;
  150. $dir = dirname($filePath);
  151. $fileName = basename($filePath);
  152. $savePath = $dir.DS.$pre.$fileName;
  153. $img->thumb($width,$height)->save($savePath);
  154. return DS.$savePath;
  155. }
  156. }