BookStack\Services\ImageService::getThumbnail PHP Method

getThumbnail() public method

If $keepRatio is true only the width will be used. Checks the cache then storage to avoid creating / accessing the filesystem on every check.
public getThumbnail ( Image $image, integer $width = 220, integer $height = 220, boolean $keepRatio = false ) : string
$image BookStack\Image
$width integer
$height integer
$keepRatio boolean
return string
    public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
    {
        $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
        $imagePath = $this->getPath($image);
        $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();
        if ($storage->exists($thumbFilePath)) {
            return $this->getPublicUrl($thumbFilePath);
        }
        try {
            $thumb = $this->imageTool->make($storage->get($imagePath));
        } catch (Exception $e) {
            if ($e instanceof \ErrorException || $e instanceof NotSupportedException) {
                throw new ImageUploadException('The server cannot create thumbnails. Please check you have the GD PHP extension installed.');
            } else {
                throw $e;
            }
        }
        if ($keepRatio) {
            $thumb->resize($width, null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
        } else {
            $thumb->fit($width, $height);
        }
        $thumbData = (string) $thumb->encode();
        $storage->put($thumbFilePath, $thumbData);
        $storage->setVisibility($thumbFilePath, 'public');
        $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 72);
        return $this->getPublicUrl($thumbFilePath);
    }