@@ -22,6 +22,7 @@ class ImageResize
2222
2323 public $ quality_jpg = 85 ;
2424 public $ quality_webp = 85 ;
25+ public $ quality_avif = 60 ;
2526 public $ quality_png = 6 ;
2627 public $ quality_truecolor = true ;
2728 public $ gamma_correct = false ;
@@ -102,50 +103,28 @@ protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
102103 */
103104 public function __construct ($ filename )
104105 {
105- if (!defined ('IMAGETYPE_WEBP ' )) {
106- define ('IMAGETYPE_WEBP ' , 18 );
107- }
108-
109- if (!defined ('IMAGETYPE_BMP ' )) {
110- define ('IMAGETYPE_BMP ' , 6 );
111- }
112-
113106 if ($ filename === null || empty ($ filename ) || (substr ($ filename , 0 , 5 ) !== 'data: ' && !is_file ($ filename ))) {
114107 throw new ImageResizeException ('File does not exist ' );
115108 }
116109
117110 $ 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- }
130111
131112 if (!$ image_info = getimagesize ($ filename , $ this ->source_info )) {
132113 $ image_info = getimagesize ($ filename );
133114 }
134115
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 ' );
142119 }
143120
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 ' );
147122 }
148123
124+ $ this ->original_w = $ image_info [0 ];
125+ $ this ->original_h = $ image_info [1 ];
126+ $ this ->source_type = $ image_info [2 ];
127+
149128 switch ($ this ->source_type ) {
150129 case IMAGETYPE_GIF :
151130 $ this ->source_image = imagecreatefromgif ($ filename );
@@ -171,10 +150,14 @@ public function __construct($filename)
171150
172151 break ;
173152
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+
174160 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- }
178161 $ this ->source_image = imagecreatefrombmp ($ filename );
179162 break ;
180163
@@ -269,9 +252,22 @@ public function save($filename, $image_type = null, $quality = null, $permission
269252 break ;
270253
271254 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 );
274263 }
264+
265+ imagealphablending ($ dest_image , false );
266+ imagesavealpha ($ dest_image , true );
267+
268+ break ;
269+
270+ case IMAGETYPE_AVIF :
275271 if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
276272 $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
277273 $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
@@ -378,16 +374,21 @@ public function save($filename, $image_type = null, $quality = null, $permission
378374 break ;
379375
380376 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- }
384377 if ($ quality === null ) {
385378 $ quality = $ this ->quality_webp ;
386379 }
387380
388381 imagewebp ($ dest_image , $ filename , $ quality );
389382 break ;
390383
384+ case IMAGETYPE_AVIF :
385+ if ($ quality === null ) {
386+ $ quality = $ this ->quality_avif ;
387+ }
388+
389+ imageavif ($ dest_image , $ filename , $ quality );
390+ break ;
391+
391392 case IMAGETYPE_PNG :
392393 if ($ quality === null || $ quality > 9 ) {
393394 $ quality = $ this ->quality_png ;
0 commit comments