- $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->getStorage($image->type);
- if ($storage->exists($thumbFilePath)) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $thumbData = $this->resizeImage($storage->get($imagePath), $width, $height, $keepRatio);
-
- $this->saveImageDataInPublicSpace($storage, $thumbFilePath, $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
- */
- protected function resizeImage(string $imageData, $width = 220, $height = null, bool $keepRatio = true)
- {
- try {
- $thumb = $this->imageTool->make($imageData);
- } catch (Exception $e) {
- if ($e instanceof ErrorException || $e instanceof NotSupportedException) {
- throw new ImageUploadException(trans('errors.cannot_create_thumbs'));
- }
-
- throw $e;
- }
-
- if ($keepRatio) {
- $thumb->resize($width, $height, function ($constraint) {
- $constraint->aspectRatio();
- $constraint->upsize();
- });
- } else {
- $thumb->fit($width, $height);
- }
-
- $thumbData = (string) $thumb->encode();
-
- // Use original image data if we're keeping the ratio
- // and the resizing does not save any space.
- if ($keepRatio && strlen($thumbData) > strlen($imageData)) {
- return $imageData;
- }
-
- return $thumbData;