Skip to content

Commit b7010cc

Browse files
committed
checking if avif works now
1 parent 690472b commit b7010cc

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

lib/ImageResize.php

+25-11
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,37 @@ public function __construct($filename)
108108
}
109109

110110
$finfo = finfo_open(FILEINFO_MIME_TYPE);
111+
$checkWebp = false;
112+
if (strstr(finfo_file($finfo, $filename), 'image') === false) {
113+
if (version_compare(PHP_VERSION, '7.0.0', '<=') && strstr(file_get_contents($filename), 'WEBPVP8') !== false) {
114+
$checkWebp = true;
115+
$this->source_type = IMAGETYPE_WEBP;
116+
} else {
117+
throw new ImageResizeException('Unsupported file type');
118+
}
119+
} elseif(strstr(finfo_file($finfo, $filename), 'image/webp') !== false) {
120+
$checkWebp = true;
121+
$this->source_type = IMAGETYPE_WEBP;
122+
}
111123

112124
if (!$image_info = getimagesize($filename, $this->source_info)) {
113125
$image_info = getimagesize($filename);
114126
}
115127

116-
if (!$image_info) {
117-
if (strstr(finfo_file($finfo, $filename), 'image') !== false) {
118-
throw new ImageResizeException('Unsupported image type');
128+
if (!$checkWebp) {
129+
if (!$image_info) {
130+
if (strstr(finfo_file($finfo, $filename), 'image') !== false) {
131+
throw new ImageResizeException('Unsupported image type');
132+
}
133+
134+
throw new ImageResizeException('Could not read file');
119135
}
120136

121-
throw new ImageResizeException('Could not read file');
137+
$this->original_w = $image_info[0];
138+
$this->original_h = $image_info[1];
139+
$this->source_type = $image_info[2];
122140
}
123141

124-
$this->original_w = $image_info[0];
125-
$this->original_h = $image_info[1];
126-
$this->source_type = $image_info[2];
127-
128142
switch ($this->source_type) {
129143
case IMAGETYPE_GIF:
130144
$this->source_image = imagecreatefromgif($filename);
@@ -152,12 +166,12 @@ public function __construct($filename)
152166

153167
case IMAGETYPE_AVIF:
154168
$this->source_image = imagecreatefromavif($filename);
155-
$this->original_w = imagesx($this->source_image);
156-
$this->original_h = imagesy($this->source_image);
157-
158169
break;
159170

160171
case IMAGETYPE_BMP:
172+
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
173+
throw new ImageResizeException('For bmp support PHP >= 7.2.0 is required');
174+
}
161175
$this->source_image = imagecreatefrombmp($filename);
162176
break;
163177

0 commit comments

Comments
 (0)