LocalAdapter.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @desc 本地适配器
  4. *
  5. * @author Tinywan(ShaoBo Wan)
  6. * @date 2022/3/7 19:54
  7. */
  8. declare(strict_types=1);
  9. namespace app\common\storage;
  10. use Intervention\Image\ImageManagerStatic as Image;
  11. use Tinywan\Storage\Adapter\AdapterAbstract;
  12. use Tinywan\Storage\Exception\StorageException;
  13. class LocalAdapter extends AdapterAbstract
  14. {
  15. /**
  16. * @desc: 方法描述
  17. *
  18. * @author Tinywan(ShaoBo Wan)
  19. */
  20. public function uploadFile(array $options = []): array
  21. {
  22. $result = [];
  23. $basePath = $this->config['root'] . $this->config['dirname'] . DIRECTORY_SEPARATOR;
  24. if (!$this->createDir($basePath)) {
  25. throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
  26. }
  27. if (!$this->createDir($basePath . '/thumb')) {
  28. throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
  29. }
  30. $baseUrl = $this->config['domain'] . $this->config['uri'] . str_replace(DIRECTORY_SEPARATOR, '/', $this->config['dirname']) . DIRECTORY_SEPARATOR;
  31. $imageType = ['jpeg', 'jpg', 'png', 'gif'];
  32. foreach ($this->files as $key => $file) {
  33. //读取文件后缀
  34. $extension = strtolower($file->getUploadExtension());
  35. if (in_array($extension, $imageType)) {
  36. // 缩略
  37. $image = Image::make($file);
  38. }
  39. $uniqueId = hash_file($this->algo, $file->getPathname());
  40. $saveFilename = $uniqueId . '.' . $file->getUploadExtension();
  41. $originSavePath = $basePath . $saveFilename;
  42. $thumbSavePath = in_array($extension, $imageType) ? ($basePath . 'thumb' . DIRECTORY_SEPARATOR . $saveFilename) : ($basePath . DIRECTORY_SEPARATOR . $saveFilename);
  43. $temp = [
  44. 'key' => $key,
  45. 'origin_name' => $file->getUploadName(),
  46. 'save_name' => $saveFilename,
  47. 'save_path' => $thumbSavePath,
  48. 'url' => $baseUrl . 'thumb' . DIRECTORY_SEPARATOR . $saveFilename,
  49. 'unique_id' => $uniqueId,
  50. 'size' => $file->getSize(),
  51. 'mime_type' => $file->getUploadMineType(),
  52. 'extension' => $file->getUploadExtension(),
  53. ];
  54. // 保存原图
  55. $file->move($originSavePath);
  56. if (in_array($extension, $imageType)) {
  57. // 保存缩略图
  58. $imgWidth = $image->width();
  59. $imgHeight = $image->height();
  60. $rate = round($imgWidth / 200, 2);
  61. $height = intval($imgHeight / $rate);
  62. $image = $image->resize(200, $height);
  63. $encoded = $image->encode('jpg');
  64. $encoded->save($thumbSavePath);
  65. }
  66. // $file->move($savePath);
  67. array_push($result, $temp);
  68. }
  69. return $result;
  70. }
  71. /**
  72. * @desc: createDir 描述
  73. */
  74. protected function createDir(string $path): bool
  75. {
  76. if (is_dir($path)) {
  77. return true;
  78. }
  79. $parent = dirname($path);
  80. if (!is_dir($parent)) {
  81. if (!$this->createDir($parent)) {
  82. return false;
  83. }
  84. }
  85. return mkdir($path);
  86. }
  87. }