Skip to content

Commit c3fd60a

Browse files
committed
avif support added
1 parent 64b7d41 commit c3fd60a

File tree

5 files changed

+52
-41
lines changed

5 files changed

+52
-41
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ If using [Composer](https://getcomposer.org/), in your `composer.json` file add:
2222
```json
2323
{
2424
"require": {
25-
"gumlet/php-image-resize": "2.0.*"
25+
"gumlet/php-image-resize": "2.1.*"
2626
}
2727
}
2828
```
@@ -31,7 +31,7 @@ If you are still using PHP 5.3, please install version ```1.7.0``` and if you ar
3131

3232
WebP support is added with PHP `5.6.0` and current version of library supports that. If you are facing issues, please use `1.9.2` version of this library.
3333

34-
For PHP versions >= 7.2, `2.0.1` or above version of this library should be used.
34+
For PHP versions >= 7.2 to 8.0, `2.0.x` or version of this library should be used.
3535

3636
Otherwise:
3737

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}],
1313
"require":
1414
{
15-
"php": ">=5.6.0",
15+
"php": ">=8.1.0",
1616
"ext-gd": "*",
1717
"ext-fileinfo": "*"
1818
},

lib/ImageResize.php

+39-38
Original file line numberDiff line numberDiff line change
@@ -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;

test/ImageResizeTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public function testLoadWebp()
6868
$this->assertInstanceOf('\Gumlet\ImageResize', $resize);
6969
}
7070

71+
public function testLoadAvif()
72+
{
73+
$image = __DIR__ . '/ressources/test_avif.avif';
74+
$resize = new ImageResize($image);
75+
76+
$this->assertEquals(100, $resize->original_w);
77+
$this->assertEquals(IMAGETYPE_AVIF, $resize->source_type);
78+
$this->assertInstanceOf('\Gumlet\ImageResize', $resize);
79+
}
80+
7181
public function testLoadBmp()
7282
{
7383
$image = __DIR__ . '/ressources/test_bmp.bmp';

test/ressources/test_avif.avif

16.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)