@@ -22,6 +22,7 @@ class ImageResize
22
22
23
23
public $ quality_jpg = 85 ;
24
24
public $ quality_webp = 85 ;
25
+ public $ quality_avif = 60 ;
25
26
public $ quality_png = 6 ;
26
27
public $ quality_truecolor = true ;
27
28
public $ gamma_correct = false ;
@@ -102,50 +103,28 @@ protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
102
103
*/
103
104
public function __construct ($ filename )
104
105
{
105
- if (!defined ('IMAGETYPE_WEBP ' )) {
106
- define ('IMAGETYPE_WEBP ' , 18 );
107
- }
108
-
109
- if (!defined ('IMAGETYPE_BMP ' )) {
110
- define ('IMAGETYPE_BMP ' , 6 );
111
- }
112
-
113
106
if ($ filename === null || empty ($ filename ) || (substr ($ filename , 0 , 5 ) !== 'data: ' && !is_file ($ filename ))) {
114
107
throw new ImageResizeException ('File does not exist ' );
115
108
}
116
109
117
110
$ finfo = finfo_open (FILEINFO_MIME_TYPE );
118
- $ checkWebp = false ;
119
- if (strstr (finfo_file ($ finfo , $ filename ), 'image ' ) === false ) {
120
- if (version_compare (PHP_VERSION , '7.0.0 ' , '<= ' ) && strstr (file_get_contents ($ filename ), 'WEBPVP8 ' ) !== false ) {
121
- $ checkWebp = true ;
122
- $ this ->source_type = IMAGETYPE_WEBP ;
123
- } else {
124
- throw new ImageResizeException ('Unsupported file type ' );
125
- }
126
- } elseif (strstr (finfo_file ($ finfo , $ filename ), 'image/webp ' ) !== false ) {
127
- $ checkWebp = true ;
128
- $ this ->source_type = IMAGETYPE_WEBP ;
129
- }
130
111
131
112
if (!$ image_info = getimagesize ($ filename , $ this ->source_info )) {
132
113
$ image_info = getimagesize ($ filename );
133
114
}
134
115
135
- if (!$ checkWebp ) {
136
- if (!$ image_info ) {
137
- if (strstr (finfo_file ($ finfo , $ filename ), 'image ' ) !== false ) {
138
- throw new ImageResizeException ('Unsupported image type ' );
139
- }
140
-
141
- throw new ImageResizeException ('Could not read file ' );
116
+ if (!$ image_info ) {
117
+ if (strstr (finfo_file ($ finfo , $ filename ), 'image ' ) !== false ) {
118
+ throw new ImageResizeException ('Unsupported image type ' );
142
119
}
143
120
144
- $ this ->original_w = $ image_info [0 ];
145
- $ this ->original_h = $ image_info [1 ];
146
- $ this ->source_type = $ image_info [2 ];
121
+ throw new ImageResizeException ('Could not read file ' );
147
122
}
148
123
124
+ $ this ->original_w = $ image_info [0 ];
125
+ $ this ->original_h = $ image_info [1 ];
126
+ $ this ->source_type = $ image_info [2 ];
127
+
149
128
switch ($ this ->source_type ) {
150
129
case IMAGETYPE_GIF :
151
130
$ this ->source_image = imagecreatefromgif ($ filename );
@@ -171,10 +150,14 @@ public function __construct($filename)
171
150
172
151
break ;
173
152
153
+ case IMAGETYPE_AVIF :
154
+ $ this ->source_image = imagecreatefromavif ($ filename );
155
+ $ this ->original_w = imagesx ($ this ->source_image );
156
+ $ this ->original_h = imagesy ($ this ->source_image );
157
+
158
+ break ;
159
+
174
160
case IMAGETYPE_BMP :
175
- if (version_compare (PHP_VERSION , '7.2.0 ' , '< ' )) {
176
- throw new ImageResizeException ('For bmp support PHP >= 7.2.0 is required ' );
177
- }
178
161
$ this ->source_image = imagecreatefrombmp ($ filename );
179
162
break ;
180
163
@@ -269,9 +252,22 @@ public function save($filename, $image_type = null, $quality = null, $permission
269
252
break ;
270
253
271
254
case IMAGETYPE_WEBP :
272
- if (version_compare (PHP_VERSION , '5.5.0 ' , '< ' )) {
273
- throw new ImageResizeException ('For WebP support PHP >= 5.5.0 is required ' );
255
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
256
+ $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
257
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
258
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ exact_size [0 ], $ exact_size [1 ], $ background );
259
+ } else {
260
+ $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
261
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
262
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
274
263
}
264
+
265
+ imagealphablending ($ dest_image , false );
266
+ imagesavealpha ($ dest_image , true );
267
+
268
+ break ;
269
+
270
+ case IMAGETYPE_AVIF :
275
271
if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
276
272
$ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
277
273
$ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
@@ -378,16 +374,21 @@ public function save($filename, $image_type = null, $quality = null, $permission
378
374
break ;
379
375
380
376
case IMAGETYPE_WEBP :
381
- if (version_compare (PHP_VERSION , '5.5.0 ' , '< ' )) {
382
- throw new ImageResizeException ('For WebP support PHP >= 5.5.0 is required ' );
383
- }
384
377
if ($ quality === null ) {
385
378
$ quality = $ this ->quality_webp ;
386
379
}
387
380
388
381
imagewebp ($ dest_image , $ filename , $ quality );
389
382
break ;
390
383
384
+ case IMAGETYPE_AVIF :
385
+ if ($ quality === null ) {
386
+ $ quality = $ this ->quality_avif ;
387
+ }
388
+
389
+ imageavif ($ dest_image , $ filename , $ quality );
390
+ break ;
391
+
391
392
case IMAGETYPE_PNG :
392
393
if ($ quality === null || $ quality > 9 ) {
393
394
$ quality = $ this ->quality_png ;
0 commit comments