Server.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use app\Request;
  4. use think\Exception;
  5. use app\common\service\ConfigService;
  6. use app\common\service\ImageCompressService;
  7. /**
  8. * 存储引擎抽象类
  9. * Class server
  10. * @package app\common\library\storage\drivers
  11. */
  12. abstract class Server
  13. {
  14. protected $file;
  15. protected $error;
  16. protected $fileName;
  17. protected $fileInfo;
  18. // 是否为内部上传
  19. protected $isInternal = false;
  20. /**
  21. * 构造函数
  22. * Server constructor.
  23. */
  24. protected function __construct()
  25. {
  26. }
  27. /**
  28. * 设置上传的文件信息
  29. * @param string $name
  30. * @throws Exception
  31. */
  32. public function setUploadFile($name)
  33. {
  34. // 接收上传的文件
  35. $this->file = app('\app\Request')->file($name);
  36. if (empty($this->file)) {
  37. throw new Exception('未找到上传文件的信息');
  38. }
  39. // 文件信息
  40. $this->fileInfo = [
  41. 'ext' => $this->file->extension(),
  42. 'size' => $this->file->getSize(),
  43. 'mime' => $this->file->getMime(),
  44. 'name' => $this->file->getOriginalName(),
  45. 'realPath' => $this->file->getRealPath(),
  46. ];
  47. // 如果是图片文件,进行压缩处理
  48. if ($this->isImageFile()) {
  49. $compressedPath = $this->compressImage();
  50. if ($compressedPath) {
  51. $this->fileInfo['realPath'] = $compressedPath;
  52. $this->fileInfo['size'] = filesize($compressedPath);
  53. }
  54. }
  55. // 生成保存文件名
  56. $this->fileName = $this->buildSaveName();
  57. }
  58. /**
  59. * 压缩图片
  60. */
  61. protected function compressImage()
  62. {
  63. // 获取压缩配置(可以从配置文件读取)
  64. $quality = ConfigService::get('upload', 'image_quality', 80);
  65. $maxWidth = ConfigService::get('upload', 'max_width', 1920);
  66. $maxHeight = ConfigService::get('upload', 'max_height', 1080);
  67. // 添加调试信息
  68. error_log('开始压缩图片: ' . $this->fileInfo['realPath']);
  69. $result = ImageCompressService::compress(
  70. $this->fileInfo['realPath'],
  71. $quality,
  72. $maxWidth,
  73. $maxHeight
  74. );
  75. if ($result === false) {
  76. error_log('图片压缩失败: ' . $this->fileInfo['realPath']);
  77. } else {
  78. error_log('图片压缩成功: ' . $result);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * 判断是否为图片文件
  84. */
  85. protected function isImageFile()
  86. {
  87. $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
  88. return in_array(strtolower($this->fileInfo['ext']), $imageExtensions);
  89. }
  90. /**
  91. * 设置上传的文件信息
  92. * @param string $filePath
  93. */
  94. public function setUploadFileByReal($filePath)
  95. {
  96. // 设置为系统内部上传
  97. $this->isInternal = true;
  98. // 文件信息
  99. $this->fileInfo = [
  100. 'name' => basename($filePath),
  101. 'size' => filesize($filePath),
  102. 'tmp_name' => $filePath,
  103. 'error' => 0,
  104. ];
  105. // 生成保存文件名
  106. $this->fileName = $this->buildSaveName();
  107. }
  108. /**
  109. * Notes: 抓取网络资源
  110. * @param $url
  111. * @param $key
  112. * @author 张无忌(2021/3/2 14:15)
  113. * @return mixed
  114. */
  115. abstract protected function fetch($url, $key);
  116. /**
  117. * 文件上传
  118. * @param $save_dir (保存路径)
  119. * @return mixed
  120. */
  121. abstract protected function upload($save_dir);
  122. /**
  123. * 文件删除
  124. * @param $fileName
  125. * @return mixed
  126. */
  127. abstract protected function delete($fileName);
  128. /**
  129. * 返回上传后文件路径
  130. * @return mixed
  131. */
  132. abstract public function getFileName();
  133. /**
  134. * 返回文件信息
  135. * @return mixed
  136. */
  137. public function getFileInfo()
  138. {
  139. return $this->fileInfo;
  140. }
  141. protected function getRealPath()
  142. {
  143. return $this->fileInfo['realPath'];
  144. }
  145. /**
  146. * 返回错误信息
  147. * @return mixed
  148. */
  149. public function getError()
  150. {
  151. return $this->error;
  152. }
  153. /**
  154. * 生成保存文件名
  155. */
  156. private function buildSaveName()
  157. {
  158. // 要上传图片的本地路径
  159. $realPath = $this->getRealPath();
  160. // 扩展名
  161. $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
  162. // 自动生成文件名
  163. return date('YmdHis') . substr(md5($realPath), 0, 5)
  164. . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
  165. }
  166. }