|
13 | 13 |
|
14 | 14 | use Intervention\Image\ImageManager;
|
15 | 15 | use kornrunner\Blurhash\Blurhash as BlurhashEncoder;
|
| 16 | +use Symfony\Contracts\Cache\CacheInterface; |
16 | 17 |
|
17 | 18 | /**
|
18 | 19 | * @author Titouan Galopin <[email protected]>
|
|
21 | 22 | */
|
22 | 23 | class BlurHash implements BlurHashInterface
|
23 | 24 | {
|
24 |
| - private $imageManager; |
25 |
| - |
26 |
| - public function __construct(?ImageManager $imageManager = null) |
27 |
| - { |
28 |
| - $this->imageManager = $imageManager; |
| 25 | + public function __construct( |
| 26 | + private ?ImageManager $imageManager = null, |
| 27 | + private ?CacheInterface $cache = null, |
| 28 | + ) { |
29 | 29 | }
|
30 | 30 |
|
31 | 31 | public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
|
@@ -62,28 +62,39 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
|
62 | 62 | throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
|
63 | 63 | }
|
64 | 64 |
|
65 |
| - // Resize image to increase encoding performance |
66 |
| - $image = $this->imageManager->make(file_get_contents($filename)); |
67 |
| - $image->resize($encodingWidth, $encodingHeight, static function ($constraint) { |
68 |
| - $constraint->aspectRatio(); |
69 |
| - $constraint->upsize(); |
70 |
| - }); |
71 |
| - |
72 |
| - // Encode using BlurHash |
73 |
| - $width = $image->getWidth(); |
74 |
| - $height = $image->getHeight(); |
75 |
| - |
76 |
| - $pixels = []; |
77 |
| - for ($y = 0; $y < $height; ++$y) { |
78 |
| - $row = []; |
79 |
| - for ($x = 0; $x < $width; ++$x) { |
80 |
| - $color = $image->pickColor($x, $y); |
81 |
| - $row[] = [$color[0], $color[1], $color[2]]; |
| 65 | + $doEncode = function (string $filename, int $encodingWidth, int $encodingHeight) { |
| 66 | + // Resize image to increase encoding performance |
| 67 | + $image = $this->imageManager->make(file_get_contents($filename)); |
| 68 | + $image->resize($encodingWidth, $encodingHeight, static function ($constraint) { |
| 69 | + $constraint->aspectRatio(); |
| 70 | + $constraint->upsize(); |
| 71 | + }); |
| 72 | + |
| 73 | + // Encode using BlurHash |
| 74 | + $width = $image->getWidth(); |
| 75 | + $height = $image->getHeight(); |
| 76 | + |
| 77 | + $pixels = []; |
| 78 | + for ($y = 0; $y < $height; ++$y) { |
| 79 | + $row = []; |
| 80 | + for ($x = 0; $x < $width; ++$x) { |
| 81 | + $color = $image->pickColor($x, $y); |
| 82 | + $row[] = [$color[0], $color[1], $color[2]]; |
| 83 | + } |
| 84 | + |
| 85 | + $pixels[] = $row; |
82 | 86 | }
|
83 | 87 |
|
84 |
| - $pixels[] = $row; |
| 88 | + return BlurhashEncoder::encode($pixels, 4, 3); |
| 89 | + }; |
| 90 | + |
| 91 | + if (null === $this->cache) { |
| 92 | + return $doEncode($filename, $encodingWidth, $encodingHeight); |
85 | 93 | }
|
86 | 94 |
|
87 |
| - return BlurhashEncoder::encode($pixels, 4, 3); |
| 95 | + return $this->cache->get( |
| 96 | + 'blurhash.'.hash('xxh3', $filename.$encodingWidth.$encodingHeight), |
| 97 | + fn () => $doEncode($filename, $encodingWidth, $encodingHeight), |
| 98 | + ); |
88 | 99 | }
|
89 | 100 | }
|
0 commit comments