location = $location; $this->imageBuilder = $imageBuilder; $this->filesystem = $filesystem; } /** * Generates and stores preview for provided website and returns the path to the image file * * @throws PreviewGenerationException */ public function generatePreview(string $url): string { /** @var Image $image */ $image = $this->imageBuilder->build(Image::class, ['url' => $url]); // If the file already exists, return its path $cacheId = sprintf('preview_%s.%s', urlencode($url), $image->type); $path = $this->location . '/' . $cacheId; if ($this->filesystem->exists($path)) { return $path; } // Save and check if an error occurred $image->saveAs($path); $error = $image->getError(); if (! empty($error)) { throw PreviewGenerationException::fromImageError($error); } // Cache the path and return it return $path; } }