Skip to content

Commit e6f66e7

Browse files
committed
bug #308 Make the blurhash encore dep optional (fabpot)
This PR was merged into the 2.x branch. Discussion ---------- Make the blurhash encore dep optional | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Tickets | n/a | License | MIT <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Features and deprecations must be submitted against branch main. --> Commits ------- 775f9a6 Make the blurhash encore dep optional
2 parents 828be1d + 775f9a6 commit e6f66e7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/LazyImage/BlurHash/BlurHash.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LazyImage\BlurHash;
1313

1414
use Intervention\Image\ImageManager;
15+
use kornrunner\Blurhash\Blurhash as BlurhashEncoder;
1516

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

3132
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string
3233
{
34+
if (!$this->imageManager) {
35+
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
36+
}
37+
if (!class_exists(BlurhashEncoder::class)) {
38+
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
39+
}
40+
3341
// Resize and encode
3442
$encoded = $this->encode($filename, $encodingWidth, $encodingHeight);
3543

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

3947
$thumbnail = $this->imageManager->canvas($width, $height);
4048
for ($y = 0; $y < $height; ++$y) {
@@ -49,7 +57,10 @@ public function createDataUriThumbnail(string $filename, int $width, int $height
4957
public function encode(string $filename, int $encodingWidth = 75, int $encodingHeight = 75): string
5058
{
5159
if (!$this->imageManager) {
52-
throw new \LogicException("Missing package, to use the \"blur_hash\" Twig function, run:\n\ncomposer require intervention/image");
60+
throw new \LogicException('To use the Blurhash feature, install intervention/image.');
61+
}
62+
if (!class_exists(BlurhashEncoder::class)) {
63+
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.');
5364
}
5465

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

77-
return \kornrunner\Blurhash\Blurhash::encode($pixels, 4, 3);
88+
return BlurhashEncoder::encode($pixels, 4, 3);
7889
}
7990
}

src/LazyImage/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
},
2828
"require": {
2929
"php": ">=7.2.5",
30-
"kornrunner/blurhash": "^1.1",
3130
"symfony/config": "^4.4.17|^5.0|^6.0",
3231
"symfony/http-kernel": "^4.4.17|^5.0|^6.0",
3332
"symfony/dependency-injection": "^4.4.17|^5.0|^6.0"
3433
},
3534
"require-dev": {
3635
"intervention/image": "^2.5",
36+
"kornrunner/blurhash": "^1.1",
3737
"symfony/framework-bundle": "^4.4.17|^5.0|^6.0",
3838
"symfony/phpunit-bridge": "^5.2|^6.0",
3939
"symfony/twig-bundle": "^4.4.17|^5.0|^6.0",

0 commit comments

Comments
 (0)