- * Save image data for the given path in the public space, if possible,
- * for the provided storage mechanism.
- */
- protected function saveImageDataInPublicSpace(Storage $storage, string $path, string $data)
- {
- $storage->put($path, $data);
-
- // Set visibility when a non-AWS-s3, s3-like storage option is in use.
- // Done since this call can break s3-like services but desired for other image stores.
- // Attempting to set ACL during above put request requires different permissions
- // hence would technically be a breaking change for actual s3 usage.
- $usingS3 = strtolower(config('filesystems.images')) === 's3';
- $usingS3Like = $usingS3 && !is_null(config('filesystems.disks.s3.endpoint'));
- if (!$usingS3Like) {
- $storage->setVisibility($path, 'public');
- }
- }
-
- /**
- * Clean up an image file name to be both URL and storage safe.
- */
- protected function cleanImageFileName(string $name): string
- {
- $name = str_replace(' ', '-', $name);
- $nameParts = explode('.', $name);
- $extension = array_pop($nameParts);
- $name = implode('-', $nameParts);
- $name = Str::slug($name);
-
- if (strlen($name) === 0) {
- $name = Str::random(10);
- }
-
- return $name . '.' . $extension;
- }
-
- /**
- * Checks if the image is a gif. Returns true if it is, else false.
- */
- protected function isGif(Image $image): bool
- {
- return strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'gif';
- }
-
- /**
- * Get the thumbnail for an image.
- * If $keepRatio is true only the width will be used.
- * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
- *
- * @param Image $image
- * @param int $width
- * @param int $height
- * @param bool $keepRatio
- *
- * @throws Exception
- * @throws ImageUploadException
- *
- * @return string
- */
- public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
- {
- 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);
-
- if ($this->cache->has('images-' . $image->id . '-' . $thumbFilePath) && $this->cache->get('images-' . $thumbFilePath)) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $storage = $this->getStorageDisk($image->type);
- if ($storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $thumbData = $this->resizeImage($storage->get($this->adjustPathForStorageDisk($imagePath, $image->type)), $width, $height, $keepRatio);
-
- $this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($thumbFilePath, $image->type), $thumbData);
- $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 60 * 72);
-
- return $this->getPublicUrl($thumbFilePath);
- }
-
- /**
- * Resize image data.
- *
- * @param string $imageData
- * @param int $width
- * @param int $height
- * @param bool $keepRatio
- *
- * @throws ImageUploadException
- *
- * @return string