Skip to content

Commit baaa7dd

Browse files
committed
[LazyImage] support intervention/image 3.0
1 parent 501ea55 commit baaa7dd

File tree

5 files changed

+81
-20
lines changed

5 files changed

+81
-20
lines changed

src/LazyImage/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 59 additions & 10 deletions
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,6 +31,11 @@ public function __construct(
2831
) {
2932
}
3033

34+
public static function intervention3(): bool
35+
{
36+
return !class_exists(ImageManagerStatic::class);
37+
}
38+
3139
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
3240
{
3341
// Resize and encode
@@ -36,14 +44,7 @@ public function createDataUriThumbnail(string $filename, int $width, int $height
3644
// Create a new blurred thumbnail from encoded BlurHash
3745
$pixels = BlurhashEncoder::decode($encoded, $width, $height);
3846

39-
$thumbnail = $this->imageManager->canvas($width, $height);
40-
for ($y = 0; $y < $height; ++$y) {
41-
for ($x = 0; $x < $width; ++$x) {
42-
$thumbnail->pixel($pixels[$y][$x], $x, $y);
43-
}
44-
}
45-
46-
return 'data:image/jpeg;base64,'.base64_encode($thumbnail->encode('jpg', 80));
47+
return $this->encodeImage($pixels, $width, $height);
4748
}
4849

4950
public function encode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
@@ -68,6 +69,29 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
6869
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
6970
}
7071

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+
7195
// Resize image to increase encoding performance
7296
$image = $this->imageManager->make(file_get_contents($filename));
7397
$image->resize($encodingWidth, $encodingHeight, static function ($constraint) {
@@ -78,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
78102
// Encode using BlurHash
79103
$width = $image->getWidth();
80104
$height = $image->getHeight();
81-
82105
$pixels = [];
106+
83107
for ($y = 0; $y < $height; ++$y) {
84108
$row = [];
85109
for ($x = 0; $x < $width; ++$x) {
@@ -90,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
90114
$pixels[] = $row;
91115
}
92116

93-
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));
94143
}
95144
}

src/LazyImage/src/DependencyInjection/LazyImageExtension.php

Lines changed: 2 additions & 0 deletions
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,6 +39,7 @@ 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
}

0 commit comments

Comments
 (0)