Skip to content

Commit 80b36bc

Browse files
committed
feature #1766 [LazyImage] refactor to twig.runtime & support intervention/image 3 (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [LazyImage] refactor to `twig.runtime` & support `intervention/image` 3 | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | Fix #1465 | License | MIT - Refactored the code to use `twig.runtime` - Support `intervention/image` 3 Commits ------- 83f3e2f [LazyImage] support `intervention/image` 3.0
2 parents a93dd4d + 83f3e2f commit 80b36bc

File tree

5 files changed

+106
-31
lines changed

5 files changed

+106
-31
lines changed

src/LazyImage/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.17.0
4+
5+
- Add support for `intervention/image` 3.0+
6+
37
## 2.13.2
48

59
- Revert "Change JavaScript package to `type: module`"

src/LazyImage/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"symfony/dependency-injection": "^5.4|^6.0|^7.0"
3535
},
3636
"require-dev": {
37-
"intervention/image": "^2.5",
37+
"intervention/image": "^2.5|^3.0",
3838
"kornrunner/blurhash": "^1.1",
3939
"symfony/cache-contracts": "^2.2",
4040
"symfony/framework-bundle": "^5.4|^6.0|^7.0",

src/LazyImage/src/BlurHash/BlurHash.php

+67-24
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Symfony\UX\LazyImage\BlurHash;
1313

14+
use Intervention\Image\Colors\Rgb\Color;
15+
use Intervention\Image\Drivers\Gd\Encoders\JpegEncoder;
1416
use Intervention\Image\ImageManager;
17+
use Intervention\Image\ImageManagerStatic;
1518
use kornrunner\Blurhash\Blurhash as BlurhashEncoder;
1619
use Symfony\Contracts\Cache\CacheInterface;
1720

@@ -28,40 +31,24 @@ public function __construct(
2831
) {
2932
}
3033

31-
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
34+
public static function intervention3(): bool
3235
{
33-
if (!$this->imageManager) {
34-
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
35-
}
36-
if (!class_exists(BlurhashEncoder::class)) {
37-
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
38-
}
36+
return !class_exists(ImageManagerStatic::class);
37+
}
3938

39+
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
40+
{
4041
// Resize and encode
4142
$encoded = $this->encode($filename, $encodingWidth, $encodingHeight);
4243

4344
// Create a new blurred thumbnail from encoded BlurHash
4445
$pixels = BlurhashEncoder::decode($encoded, $width, $height);
4546

46-
$thumbnail = $this->imageManager->canvas($width, $height);
47-
for ($y = 0; $y < $height; ++$y) {
48-
for ($x = 0; $x < $width; ++$x) {
49-
$thumbnail->pixel($pixels[$y][$x], $x, $y);
50-
}
51-
}
52-
53-
return 'data:image/jpeg;base64,'.base64_encode($thumbnail->encode('jpg', 80));
47+
return $this->encodeImage($pixels, $width, $height);
5448
}
5549

5650
public function encode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
5751
{
58-
if (!$this->imageManager) {
59-
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
60-
}
61-
if (!class_exists(BlurhashEncoder::class)) {
62-
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
63-
}
64-
6552
if ($this->cache) {
6653
return $this->cache->get(
6754
'blurhash.'.hash('xxh3', $filename.$encodingWidth.$encodingHeight),
@@ -74,6 +61,37 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
7461

7562
private function doEncode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
7663
{
64+
if (!$this->imageManager) {
65+
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
66+
}
67+
68+
if (!class_exists(BlurhashEncoder::class)) {
69+
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
70+
}
71+
72+
return BlurhashEncoder::encode($this->generatePixels($filename, $encodingWidth, $encodingHeight), 4, 3);
73+
}
74+
75+
private function generatePixels(string $filename, int $encodingWidth, int $encodingHeight): array
76+
{
77+
if (self::intervention3()) {
78+
$image = $this->imageManager->read($filename)->scale($encodingWidth, $encodingHeight);
79+
$width = $image->width();
80+
$height = $image->height();
81+
$pixels = [];
82+
83+
for ($y = 0; $y < $height; ++$y) {
84+
$row = [];
85+
for ($x = 0; $x < $width; ++$x) {
86+
$row[] = $image->pickColor($x, $y)->toArray();
87+
}
88+
89+
$pixels[] = $row;
90+
}
91+
92+
return $pixels;
93+
}
94+
7795
// Resize image to increase encoding performance
7896
$image = $this->imageManager->make(file_get_contents($filename));
7997
$image->resize($encodingWidth, $encodingHeight, static function ($constraint) {
@@ -84,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
84102
// Encode using BlurHash
85103
$width = $image->getWidth();
86104
$height = $image->getHeight();
87-
88105
$pixels = [];
106+
89107
for ($y = 0; $y < $height; ++$y) {
90108
$row = [];
91109
for ($x = 0; $x < $width; ++$x) {
@@ -96,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
96114
$pixels[] = $row;
97115
}
98116

99-
return BlurhashEncoder::encode($pixels, 4, 3);
117+
return $pixels;
118+
}
119+
120+
private function encodeImage(array $pixels, int $width, int $height): string
121+
{
122+
if (self::intervention3()) {
123+
$thumbnail = $this->imageManager->create($width, $height);
124+
125+
for ($y = 0; $y < $height; ++$y) {
126+
for ($x = 0; $x < $width; ++$x) {
127+
$thumbnail->drawPixel($x, $y, new Color($pixels[$y][$x][0], $pixels[$y][$x][1], $pixels[$y][$x][2]));
128+
}
129+
}
130+
131+
return $thumbnail->encode(new JpegEncoder(80))->toDataUri();
132+
}
133+
134+
$thumbnail = $this->imageManager->canvas($width, $height);
135+
136+
for ($y = 0; $y < $height; ++$y) {
137+
for ($x = 0; $x < $width; ++$x) {
138+
$thumbnail->pixel($pixels[$y][$x], $x, $y);
139+
}
140+
}
141+
142+
return 'data:image/jpeg;base64,'.base64_encode($thumbnail->encode('jpg', 80));
100143
}
101144
}

src/LazyImage/src/DependencyInjection/LazyImageExtension.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LazyImage\DependencyInjection;
1313

14+
use Intervention\Image\Drivers\Gd\Driver;
1415
use Intervention\Image\ImageManager;
1516
use Symfony\Component\AssetMapper\AssetMapperInterface;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -38,14 +39,14 @@ public function load(array $configs, ContainerBuilder $container)
3839
if (class_exists(ImageManager::class)) {
3940
$container
4041
->setDefinition('lazy_image.image_manager', new Definition(ImageManager::class))
42+
->addArgument(BlurHash::intervention3() ? Driver::class : [])
4143
->setPublic(false)
4244
;
4345
}
4446

4547
$container
4648
->setDefinition('lazy_image.blur_hash', new Definition(BlurHash::class))
4749
->setArgument(0, new Reference('lazy_image.image_manager', ContainerInterface::NULL_ON_INVALID_REFERENCE))
48-
->setPublic(false)
4950
;
5051

5152
if (isset($config['cache'])) {

0 commit comments

Comments
 (0)