- public function getThumbnail(
- Image $image,
- ?int $width,
- ?int $height,
- bool $keepRatio = false,
- bool $shouldCreate = false,
- bool $canCreate = false,
- ): ?string {
- // Do not resize GIF images where we're not cropping
- if ($keepRatio && $this->isGif($image)) {
- return $this->storage->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 && !$shouldCreate) {
- return $this->storage->getPublicUrl($cachedThumbPath);
- }
-
- // If thumbnail has already been generated, serve that and cache path
- $disk = $this->storage->getDisk($image->type);
- if (!$shouldCreate && $disk->exists($this->storage->adjustPathForDisk($thumbFilePath, $image->type))) {
- $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
-
- return $this->storage->getPublicUrl($thumbFilePath);
- }
-
- $imageData = $disk->get($this->storage->adjustPathForDisk($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->storage->getPublicUrl($image->path);
- }
-
- if (!$shouldCreate && !$canCreate) {
- return null;
- }
-
- // If not in cache and thumbnail does not exist, generate thumb and cache path
- $thumbData = $this->resizeImage($imageData, $width, $height, $keepRatio);
- $this->storage->storeInPublicSpace($disk, $this->storage->adjustPathForDisk($thumbFilePath, $image->type), $thumbData);
- $this->cache->put($thumbCacheKey, $thumbFilePath, 60 * 60 * 72);
-
- return $this->storage->getPublicUrl($thumbFilePath);
- }
-
- /**
- * Resize the image of given data to the specified size, and return the new image data.
- *
- * @throws ImageUploadException
- */
- protected function resizeImage(string $imageData, ?int $width, ?int $height, bool $keepRatio): string