metadata.php: add sniff_image

This commit is contained in:
Zankaria 2025-03-25 22:43:04 +01:00
parent 86962c664f
commit 75343c1af1

View file

@ -34,3 +34,26 @@ function ext_to_mime(string $ext): ?string {
];
return $ext_to_mime[$ext] ?? null;
}
/**
* Sniffs the image content.
*
* @param string $file_path The path to an image file.
* @return array An array with the image's width, height and mime type.
* @throws \RuntimeException On error, or on non-image files.
*/
function sniff_image(string $file_path): array {
$ret = \getimagesize($file_path);
if ($ret === false) {
throw new \RuntimeException("Could not read image sizes of '$file_path'");
}
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
throw new \RuntimeException("Error '$file_path' is not an image");
}
$width = $ret[0];
$height = $ret[1];
$mime = $ret['mime'];
return [ $width, $height, $mime ];
}