11
11
12
12
namespace Symfony \UX \LazyImage \BlurHash ;
13
13
14
+ use Intervention \Image \Colors \Rgb \Color ;
15
+ use Intervention \Image \Drivers \Gd \Encoders \JpegEncoder ;
14
16
use Intervention \Image \ImageManager ;
17
+ use Intervention \Image \ImageManagerStatic ;
15
18
use kornrunner \Blurhash \Blurhash as BlurhashEncoder ;
16
19
use Symfony \Contracts \Cache \CacheInterface ;
17
20
@@ -28,6 +31,11 @@ public function __construct(
28
31
) {
29
32
}
30
33
34
+ public static function intervention3 (): bool
35
+ {
36
+ return !class_exists (ImageManagerStatic::class);
37
+ }
38
+
31
39
public function createDataUriThumbnail (string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
32
40
{
33
41
// Resize and encode
@@ -36,14 +44,7 @@ public function createDataUriThumbnail(string $filename, int $width, int $height
36
44
// Create a new blurred thumbnail from encoded BlurHash
37
45
$ pixels = BlurhashEncoder::decode ($ encoded , $ width , $ height );
38
46
39
- $ thumbnail = $ this ->imageManager ->canvas ($ width , $ height );
40
- for ($ y = 0 ; $ y < $ height ; ++$ y ) {
41
- for ($ x = 0 ; $ x < $ width ; ++$ x ) {
42
- $ thumbnail ->pixel ($ pixels [$ y ][$ x ], $ x , $ y );
43
- }
44
- }
45
-
46
- return 'data:image/jpeg;base64, ' .base64_encode ($ thumbnail ->encode ('jpg ' , 80 ));
47
+ return $ this ->encodeImage ($ pixels , $ width , $ height );
47
48
}
48
49
49
50
public function encode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
@@ -68,6 +69,29 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
68
69
throw new \LogicException ('To use the Blurhash feature, install kornrunner/blurhash. ' );
69
70
}
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
+
71
95
// Resize image to increase encoding performance
72
96
$ image = $ this ->imageManager ->make (file_get_contents ($ filename ));
73
97
$ image ->resize ($ encodingWidth , $ encodingHeight , static function ($ constraint ) {
@@ -78,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
78
102
// Encode using BlurHash
79
103
$ width = $ image ->getWidth ();
80
104
$ height = $ image ->getHeight ();
81
-
82
105
$ pixels = [];
106
+
83
107
for ($ y = 0 ; $ y < $ height ; ++$ y ) {
84
108
$ row = [];
85
109
for ($ x = 0 ; $ x < $ width ; ++$ x ) {
@@ -90,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
90
114
$ pixels [] = $ row ;
91
115
}
92
116
93
- 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 ));
94
143
}
95
144
}
0 commit comments