1111
1212namespace Symfony \UX \LazyImage \BlurHash ;
1313
14+ use Intervention \Image \Colors \Rgb \Color ;
15+ use Intervention \Image \Drivers \Gd \Encoders \JpegEncoder ;
1416use Intervention \Image \ImageManager ;
17+ use Intervention \Image \ImageManagerStatic ;
1518use kornrunner \Blurhash \Blurhash as BlurhashEncoder ;
1619use Symfony \Contracts \Cache \CacheInterface ;
1720
@@ -28,40 +31,24 @@ public function __construct(
2831 ) {
2932 }
3033
31- public function createDataUriThumbnail ( string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
34+ public static function intervention3 ( ): bool
3235 {
33- if (!$ this ->imageManager ) {
34- throw new \LogicException ('To use the Blurhash feature, install intervention/image. ' );
35- }
36- if (!class_exists (BlurhashEncoder::class)) {
37- throw new \LogicException ('To use the Blurhash feature, install kornrunner/blurhash. ' );
38- }
36+ return !class_exists (ImageManagerStatic::class);
37+ }
3938
39+ public function createDataUriThumbnail (string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
40+ {
4041 // Resize and encode
4142 $ encoded = $ this ->encode ($ filename , $ encodingWidth , $ encodingHeight );
4243
4344 // Create a new blurred thumbnail from encoded BlurHash
4445 $ pixels = BlurhashEncoder::decode ($ encoded , $ width , $ height );
4546
46- $ thumbnail = $ this ->imageManager ->canvas ($ width , $ height );
47- for ($ y = 0 ; $ y < $ height ; ++$ y ) {
48- for ($ x = 0 ; $ x < $ width ; ++$ x ) {
49- $ thumbnail ->pixel ($ pixels [$ y ][$ x ], $ x , $ y );
50- }
51- }
52-
53- return 'data:image/jpeg;base64, ' .base64_encode ($ thumbnail ->encode ('jpg ' , 80 ));
47+ return $ this ->encodeImage ($ pixels , $ width , $ height );
5448 }
5549
5650 public function encode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
5751 {
58- if (!$ this ->imageManager ) {
59- throw new \LogicException ('To use the Blurhash feature, install intervention/image. ' );
60- }
61- if (!class_exists (BlurhashEncoder::class)) {
62- throw new \LogicException ('To use the Blurhash feature, install kornrunner/blurhash. ' );
63- }
64-
6552 if ($ this ->cache ) {
6653 return $ this ->cache ->get (
6754 'blurhash. ' .hash ('xxh3 ' , $ filename .$ encodingWidth .$ encodingHeight ),
@@ -74,6 +61,37 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
7461
7562 private function doEncode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
7663 {
64+ if (!$ this ->imageManager ) {
65+ throw new \LogicException ('To use the Blurhash feature, install intervention/image. ' );
66+ }
67+
68+ if (!class_exists (BlurhashEncoder::class)) {
69+ throw new \LogicException ('To use the Blurhash feature, install kornrunner/blurhash. ' );
70+ }
71+
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+
7795 // Resize image to increase encoding performance
7896 $ image = $ this ->imageManager ->make (file_get_contents ($ filename ));
7997 $ image ->resize ($ encodingWidth , $ encodingHeight , static function ($ constraint ) {
@@ -84,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
84102 // Encode using BlurHash
85103 $ width = $ image ->getWidth ();
86104 $ height = $ image ->getHeight ();
87-
88105 $ pixels = [];
106+
89107 for ($ y = 0 ; $ y < $ height ; ++$ y ) {
90108 $ row = [];
91109 for ($ x = 0 ; $ x < $ width ; ++$ x ) {
@@ -96,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
96114 $ pixels [] = $ row ;
97115 }
98116
99- 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 ));
100143 }
101144}
0 commit comments