- /**
- * Get the thumbnail for an image.
- * If $keepRatio is true only the width will be used.
- * @param $image
- * @param int $width
- * @param int $height
- * @param bool $keepRatio
- * @return string
- */
- public function getThumbnail($image, $width = 220, $height = 220, $keepRatio = false)
- {
- $explodedPath = explode('/', $image->url);
- $dirPrefix = $keepRatio ? 'scaled-' : 'thumbs-';
- array_splice($explodedPath, 4, 0, [$dirPrefix . $width . '-' . $height]);
- $thumbPath = implode('/', $explodedPath);
- $thumbFilePath = public_path() . $thumbPath;
-
- // Return the thumbnail url path if already exists
- if (file_exists($thumbFilePath)) {
- return $thumbPath;
- }
-
- // Otherwise create the thumbnail
- $thumb = ImageTool::make(public_path() . $image->url);
- if($keepRatio) {
- $thumb->resize($width, null, function ($constraint) {
- $constraint->aspectRatio();
- $constraint->upsize();
- });
- } else {
- $thumb->fit($width, $height);
- }
-
- // Create thumbnail folder if it does not exist
- if (!file_exists(dirname($thumbFilePath))) {
- mkdir(dirname($thumbFilePath), 0775, true);
- }
-
- //Save Thumbnail
- $thumb->save($thumbFilePath);
- return $thumbPath;
- }