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,40 +31,24 @@ public function __construct(
28
31
) {
29
32
}
30
33
31
- public function createDataUriThumbnail ( string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
34
+ public static function intervention3 ( ): bool
32
35
{
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
+ }
39
38
39
+ public function createDataUriThumbnail (string $ filename , int $ width , int $ height , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
40
+ {
40
41
// Resize and encode
41
42
$ encoded = $ this ->encode ($ filename , $ encodingWidth , $ encodingHeight );
42
43
43
44
// Create a new blurred thumbnail from encoded BlurHash
44
45
$ pixels = BlurhashEncoder::decode ($ encoded , $ width , $ height );
45
46
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 );
54
48
}
55
49
56
50
public function encode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
57
51
{
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
-
65
52
if ($ this ->cache ) {
66
53
return $ this ->cache ->get (
67
54
'blurhash. ' .hash ('xxh3 ' , $ filename .$ encodingWidth .$ encodingHeight ),
@@ -74,6 +61,37 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH
74
61
75
62
private function doEncode (string $ filename , int $ encodingWidth = 75 , int $ encodingHeight = 75 ): string
76
63
{
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
+
77
95
// Resize image to increase encoding performance
78
96
$ image = $ this ->imageManager ->make (file_get_contents ($ filename ));
79
97
$ image ->resize ($ encodingWidth , $ encodingHeight , static function ($ constraint ) {
@@ -84,8 +102,8 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
84
102
// Encode using BlurHash
85
103
$ width = $ image ->getWidth ();
86
104
$ height = $ image ->getHeight ();
87
-
88
105
$ pixels = [];
106
+
89
107
for ($ y = 0 ; $ y < $ height ; ++$ y ) {
90
108
$ row = [];
91
109
for ($ x = 0 ; $ x < $ width ; ++$ x ) {
@@ -96,6 +114,31 @@ private function doEncode(string $filename, int $encodingWidth = 75, int $encodi
96
114
$ pixels [] = $ row ;
97
115
}
98
116
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 ));
100
143
}
101
144
}
0 commit comments