UploadService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = PUBILC_PATH.'uploads';
  73. return ltrim($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. echo $path;echo $dir;
  91. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  92. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  93. $file = request()->file($fileName);
  94. if($autoValidate) $file = $file->validate(self::$imageValidate);
  95. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  96. if(false === $fileInfo) return self::setError($file->getError());
  97. return self::successful($path,$fileInfo);
  98. }
  99. /**
  100. * 文件上传
  101. * @param string $fileName 上传文件名
  102. * @param string $path 上传路径
  103. * @param bool $moveName 生成文件名
  104. * @param bool $autoValidate 验证规则 [size:1024,ext:[],type:[]]
  105. * @param null $root 上传根目录路径
  106. * @param string $rule 文件名自动生成规则
  107. * @return mixed
  108. */
  109. public static function file($fileName, $path, $moveName = true, $autoValidate=[], $root=null, $rule='uniqid')
  110. {
  111. self::init();
  112. $path = self::uploadDir($path,$root);
  113. $dir = ROOT_PATH . $path;
  114. if(!self::validDir($dir)) return self::setError('生成上传目录失败,请检查权限!');
  115. if(!isset($_FILES[$fileName])) return self::setError('上传文件不存在!');
  116. $extension = strtolower(pathinfo($_FILES[$fileName]['name'], PATHINFO_EXTENSION));
  117. if(strtolower($extension) == 'php' || !$extension)
  118. return self::setError('上传文件非法!');
  119. $file = request()->file($fileName);
  120. if(count($autoValidate)>0) $file = $file->validate($autoValidate);
  121. $fileInfo = $file->rule($rule)->move($dir,$moveName);
  122. if(false === $fileInfo) return self::setError($file->getError());
  123. return self::successful($path,$fileInfo);
  124. }
  125. public static function pathToUrl($path)
  126. {
  127. return trim(str_replace(DS, '/', $path),'.');
  128. }
  129. public static function openImage($filePath)
  130. {
  131. return \think\Image::open($filePath);
  132. }
  133. /**
  134. * 图片压缩
  135. *
  136. * @param string $filePath 文件路径
  137. * @param int $ratio 缩放比例 1-9
  138. * @param string $pre 前缀
  139. * @return string 压缩图片路径
  140. */
  141. public static function thumb($filePath, $ratio=8, $pre='s_')
  142. {
  143. $filePath = '.'.ltrim($filePath,'.');
  144. $img = self::openImage($filePath);
  145. $width = $img->width() * $ratio / 10;
  146. $height = $img->height() * $ratio / 10;
  147. $dir = dirname($filePath);
  148. $fileName = basename($filePath);
  149. $savePath = $dir.DS.$pre.$fileName;
  150. $img->thumb($width,$height)->save($savePath);
  151. return ltrim($savePath,'.');
  152. }
  153. }