Skip to content

[LazyImage] refactor to twig.runtime & support intervention/image 3 #1766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/LazyImage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.17.0

- Add support for `intervention/image` 3.0+

## 2.13.2

- Revert "Change JavaScript package to `type: module`"
Expand Down
2 changes: 1 addition & 1 deletion src/LazyImage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"symfony/dependency-injection": "^5.4|^6.0|^7.0"
},
"require-dev": {
"intervention/image": "^2.5",
"intervention/image": "^2.5|^3.0",
"kornrunner/blurhash": "^1.1",
"symfony/cache-contracts": "^2.2",
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
Expand Down
91 changes: 67 additions & 24 deletions src/LazyImage/src/BlurHash/BlurHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\UX\LazyImage\BlurHash;

use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Drivers\Gd\Encoders\JpegEncoder;
use Intervention\Image\ImageManager;
use Intervention\Image\ImageManagerStatic;
use kornrunner\Blurhash\Blurhash as BlurhashEncoder;
use Symfony\Contracts\Cache\CacheInterface;

Expand All @@ -28,40 +31,24 @@ public function __construct(
) {
}

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

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

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

$thumbnail = $this->imageManager->canvas($width, $height);
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
$thumbnail->pixel($pixels[$y][$x], $x, $y);
}
}

return 'data:image/jpeg;base64,'.base64_encode($thumbnail->encode('jpg', 80));
return $this->encodeImage($pixels, $width, $height);
}

public function encode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
{
if (!$this->imageManager) {
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
}
if (!class_exists(BlurhashEncoder::class)) {
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
}

if ($this->cache) {
return $this->cache->get(
'blurhash.'.hash('xxh3', $filename.$encodingWidth.$encodingHeight),
Expand All @@ -74,6 +61,37 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH

private function doEncode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
{
if (!$this->imageManager) {
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
}

if (!class_exists(BlurhashEncoder::class)) {
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
}

return BlurhashEncoder::encode($this->generatePixels($filename, $encodingWidth, $encodingHeight), 4, 3);
}

private function generatePixels(string $filename, int $encodingWidth, int $encodingHeight): array
{
if (self::intervention3()) {
$image = $this->imageManager->read($filename)->scale($encodingWidth, $encodingHeight);
$width = $image->width();
$height = $image->height();
$pixels = [];

for ($y = 0; $y < $height; ++$y) {
$row = [];
for ($x = 0; $x < $width; ++$x) {
$row[] = $image->pickColor($x, $y)->toArray();
}

$pixels[] = $row;
}

return $pixels;
}

// Resize image to increase encoding performance
$image = $this->imageManager->make(file_get_contents($filename));
$image->resize($encodingWidth, $encodingHeight, static function ($constraint) {
Expand All @@ -84,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
// Encode using BlurHash
$width = $image->getWidth();
$height = $image->getHeight();

$pixels = [];

for ($y = 0; $y < $height; ++$y) {
$row = [];
for ($x = 0; $x < $width; ++$x) {
Expand All @@ -96,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
$pixels[] = $row;
}

return BlurhashEncoder::encode($pixels, 4, 3);
return $pixels;
}

private function encodeImage(array $pixels, int $width, int $height): string
{
if (self::intervention3()) {
$thumbnail = $this->imageManager->create($width, $height);

for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
$thumbnail->drawPixel($x, $y, new Color($pixels[$y][$x][0], $pixels[$y][$x][1], $pixels[$y][$x][2]));
}
}

return $thumbnail->encode(new JpegEncoder(80))->toDataUri();
}

$thumbnail = $this->imageManager->canvas($width, $height);

for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
$thumbnail->pixel($pixels[$y][$x], $x, $y);
}
}

return 'data:image/jpeg;base64,'.base64_encode($thumbnail->encode('jpg', 80));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\LazyImage\DependencyInjection;

use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -38,14 +39,14 @@ public function load(array $configs, ContainerBuilder $container)
if (class_exists(ImageManager::class)) {
$container
->setDefinition('lazy_image.image_manager', new Definition(ImageManager::class))
->addArgument(BlurHash::intervention3() ? Driver::class : [])
->setPublic(false)
;
}

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

if (isset($config['cache'])) {
Expand Down
Loading