- * @throws InvalidArgumentException
- */
- public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false): string
- {
- // Do not resize GIF images where we're not cropping
- if ($keepRatio && $this->isGif($image)) {
- return $this->getPublicUrl($image->path);
- }
-
- $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
- $imagePath = $image->path;
- $thumbFilePath = dirname($imagePath) . $thumbDirName . basename($imagePath);
-
- $thumbCacheKey = 'images::' . $image->id . '::' . $thumbFilePath;
-
- // Return path if in cache
- $cachedThumbPath = $this->cache->get($thumbCacheKey);
- if ($cachedThumbPath) {
- return $this->getPublicUrl($cachedThumbPath);
- }
-
- // If thumbnail has already been generated, serve that and cache path
- $storage = $this->getStorageDisk($image->type);
- if ($storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
- $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
-
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $imageData = $storage->get($this->adjustPathForStorageDisk($imagePath, $image->type));
-
- // Do not resize apng images where we're not cropping
- if ($keepRatio && $this->isApngData($image, $imageData)) {
- $this->cache->put($thumbCacheKey, $image->path, 60 * 60 * 72);
-
- return $this->getPublicUrl($image->path);
- }
-
- // If not in cache and thumbnail does not exist, generate thumb and cache path
- $thumbData = $this->resizeImage($imageData, $width, $height, $keepRatio);
- $this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($thumbFilePath, $image->type), $thumbData);
- $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
-
- return $this->getPublicUrl($thumbFilePath);
- }
-
- /**
- * Resize the image of given data to the specified size, and return the new image data.
- *
- * @throws ImageUploadException