1 <?php namespace BookStack\Services;
4 use Intervention\Image\ImageManager;
5 use Illuminate\Contracts\Filesystem\Factory as FileSystem;
6 use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
7 use Illuminate\Contracts\Cache\Repository as Cache;
9 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 protected $fileSystem;
19 * @var FileSystemInstance
21 protected $storageInstance;
22 protected $storageUrl;
25 * ImageService constructor.
30 public function __construct(ImageManager $imageTool, FileSystem $fileSystem, Cache $cache)
32 $this->imageTool = $imageTool;
33 $this->fileSystem = $fileSystem;
34 $this->cache = $cache;
37 public function saveNew(Image $image, UploadedFile $uploadedFile, $type)
39 $storage = $this->getStorage();
40 $secureUploads = Setting::get('app-secure-images');
41 $imageName = str_replace(' ', '-', $uploadedFile->getClientOriginalName());
43 if ($secureUploads) $imageName = str_random(16) . '-' . $imageName;
45 $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/';
46 while ($storage->exists($imagePath . $imageName)) {
47 $imageName = str_random(3) . $imageName;
49 $fullPath = $imagePath . $imageName;
51 $storage->put($fullPath, file_get_contents($uploadedFile->getRealPath()));
53 $userId = auth()->user()->id;
54 $image = $image->forceCreate([
57 'url' => $this->getPublicUrl($fullPath),
59 'created_by' => $userId,
60 'updated_by' => $userId
67 * Get the thumbnail for an image.
68 * If $keepRatio is true only the width will be used.
69 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
74 * @param bool $keepRatio
77 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
79 $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
80 $thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path);
82 if ($this->cache->has('images-' . $image->id . '-' . $thumbFilePath) && $this->cache->get('images-' . $thumbFilePath)) {
83 return $this->getPublicUrl($thumbFilePath);
86 $storage = $this->getStorage();
88 if ($storage->exists($thumbFilePath)) {
89 return $this->getPublicUrl($thumbFilePath);
92 // Otherwise create the thumbnail
93 $thumb = $this->imageTool->make($storage->get($image->path));
95 $thumb->resize($width, null, function ($constraint) {
96 $constraint->aspectRatio();
97 $constraint->upsize();
100 $thumb->fit($width, $height);
103 $thumbData = (string)$thumb->encode();
104 $storage->put($thumbFilePath, $thumbData);
105 $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 72);
107 return $this->getPublicUrl($thumbFilePath);
111 * Destroys an Image object along with its files and thumbnails.
112 * @param Image $image
115 public function destroyImage(Image $image)
117 $storage = $this->getStorage();
119 $imageFolder = dirname($image->path);
120 $imageFileName = basename($image->path);
121 $allImages = collect($storage->allFiles($imageFolder));
123 $imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
124 $expectedIndex = strlen($imagePath) - strlen($imageFileName);
125 return strpos($imagePath, $imageFileName) === $expectedIndex;
128 $storage->delete($imagesToDelete->all());
130 // Cleanup of empty folders
131 foreach ($storage->directories($imageFolder) as $directory) {
132 if ($this->isFolderEmpty($directory)) $storage->deleteDirectory($directory);
134 if ($this->isFolderEmpty($imageFolder)) $storage->deleteDirectory($imageFolder);
141 * Get the storage that will be used for storing images.
142 * @return FileSystemInstance
144 private function getStorage()
146 if ($this->storageInstance !== null) return $this->storageInstance;
148 $storageType = env('STORAGE_TYPE');
149 $this->storageInstance = $this->fileSystem->disk($storageType);
151 return $this->storageInstance;
155 * Check whether or not a folder is empty.
159 private function isFolderEmpty($path)
161 $files = $this->getStorage()->files($path);
162 $folders = $this->getStorage()->directories($path);
163 return count($files) === 0 && count($folders) === 0;
167 * Gets a public facing url for an image by checking relevant environment variables.
171 private function getPublicUrl($filePath)
173 if ($this->storageUrl === null) {
174 $storageUrl = env('STORAGE_URL');
176 // Get the standard public s3 url if s3 is set as storage type
177 if ($storageUrl == false && env('STORAGE_TYPE') === 's3') {
178 $storageDetails = config('filesystems.disks.s3');
179 $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
182 $this->storageUrl = $storageUrl;
185 return ($this->storageUrl == false ? '' : rtrim($this->storageUrl, '/')) . $filePath;