- $storage = $this->getStorage($image->type);
- if ($storage->exists($thumbFilePath)) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $thumbData = $this->resizeImage($storage->get($imagePath), $width, $height, $keepRatio);
-
- $storage->put($thumbFilePath, $thumbData);
- $storage->setVisibility($thumbFilePath, 'public');
- $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
- * @return string
- * @throws ImageUploadException
- */
- 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;