In PHP 8, you can create an image based on the popular image types without worrying about what it is:
<?php
function imageCreateFromAny($filepath): ?\GdImage {
return match (exif_imagetype($filepath)) {
// gif
1 => imageCreateFromGif($filepath),
// jpg
2 => imageCreateFromJpeg($filepath),
// png
3 => imageCreateFromPng($filepath),
// bmp
6 => imageCreateFromBmp($filepath),
// not defined
default => null,
};
}
?>