Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active September 3, 2022 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemix/781d8f4b9d08409f76fdbb85cae52b5f to your computer and use it in GitHub Desktop.
Save mikemix/781d8f4b9d08409f76fdbb85cae52b5f to your computer and use it in GitHub Desktop.
<?php
final class RandomImageSource implements ImageSourceInterface
{
/** {@inheritDoc} */
public function __invoke(string $imageId, string $imageFormat): ImageInterface
{
return new ImmutableImage($imageId, $imageFormat, static function () use ($imageId): string {
$randomImageUrl = \sprintf('https://picsum.photos/seed/%s/1000/1000', \sha1($imageId));
$randomImageData = \file_get_contents($randomImageUrl);
if (!$randomImageData) {
throw new ImageSourceException('Could not download the image');
}
return $randomImageData;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment