12
12
namespace Symfony \UX \LazyImage \BlurHash ;
13
13
14
14
use Intervention \Image \ImageManager ;
15
+ use kornrunner \Blurhash \Blurhash as BlurhashEncoder ;
15
16
16
17
/**
17
18
* @author Titouan Galopin <[email protected] >
@@ -30,11 +31,18 @@ public function __construct(ImageManager $imageManager = null)
30
31
31
32
public function createDataUriThumbnail (string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
32
33
{
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
+
33
41
// Resize and encode
34
42
$ encoded = $ this ->encode ($ filename , $ encodingWidth , $ encodingHeight );
35
43
36
44
// Create a new blurred thumbnail from encoded BlurHash
37
- $ pixels = \ kornrunner \ Blurhash \Blurhash ::decode ($ encoded , $ width , $ height );
45
+ $ pixels = BlurhashEncoder ::decode ($ encoded , $ width , $ height );
38
46
39
47
$ thumbnail = $ this ->imageManager ->canvas ($ width , $ height );
40
48
for ($ y = 0 ; $ y < $ height ; ++$ y ) {
@@ -49,7 +57,10 @@ public function createDataUriThumbnail(string $filename, int $width, int $height
49
57
public function encode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
50
58
{
51
59
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. ' );
53
64
}
54
65
55
66
// Resize image to increase encoding performance
@@ -74,6 +85,6 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
74
85
$ pixels [] = $ row ;
75
86
}
76
87
77
- return \ kornrunner \ Blurhash \Blurhash ::encode ($ pixels , 4 , 3 );
88
+ return BlurhashEncoder ::encode ($ pixels , 4 , 3 );
78
89
}
79
90
}
0 commit comments