forked from leftypol/leftypol
metadata.php: add sniff_image
This commit is contained in:
parent
86962c664f
commit
75343c1af1
1 changed files with 23 additions and 0 deletions
|
@ -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 ];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue