diff --git a/inc/functions/metadata.php b/inc/functions/metadata.php index 4132f1dd..b7b7c665 100644 --- a/inc/functions/metadata.php +++ b/inc/functions/metadata.php @@ -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 ]; +}