Skip to content

Make the blurhash encore dep optional #308

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 1, 2022
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
17 changes: 14 additions & 3 deletions src/LazyImage/BlurHash/BlurHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\LazyImage\BlurHash;

use Intervention\Image\ImageManager;
use kornrunner\Blurhash\Blurhash as BlurhashEncoder;

/**
* @author Titouan Galopin <[email protected]>
Expand All @@ -30,11 +31,18 @@ public function __construct(ImageManager $imageManager = null)

public function createDataUriThumbnail(string $filename, int $width, int $height, 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.');
}

// Resize and encode
$encoded = $this->encode($filename, $encodingWidth, $encodingHeight);

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

$thumbnail = $this->imageManager->canvas($width, $height);
for ($y = 0; $y < $height; ++$y) {
Expand All @@ -49,7 +57,10 @@ public function createDataUriThumbnail(string $filename, int $width, int $height
public function encode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
{
if (!$this->imageManager) {
throw new \LogicException("Missing package, to use the \"blur_hash\" Twig function, run:\n\ncomposer require intervention/image");
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.');
}

// Resize image to increase encoding performance
Expand All @@ -74,6 +85,6 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
$pixels[] = $row;
}

return \kornrunner\Blurhash\Blurhash::encode($pixels, 4, 3);
return BlurhashEncoder::encode($pixels, 4, 3);
}
}
2 changes: 1 addition & 1 deletion src/LazyImage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
},
"require": {
"php": ">=7.2.5",
"kornrunner/blurhash": "^1.1",
"symfony/config": "^4.4.17|^5.0|^6.0",
"symfony/http-kernel": "^4.4.17|^5.0|^6.0",
"symfony/dependency-injection": "^4.4.17|^5.0|^6.0"
},
"require-dev": {
"intervention/image": "^2.5",
"kornrunner/blurhash": "^1.1",
"symfony/framework-bundle": "^4.4.17|^5.0|^6.0",
"symfony/phpunit-bridge": "^5.2|^6.0",
"symfony/twig-bundle": "^4.4.17|^5.0|^6.0",
Expand Down