I just care if it's any of the common formats so that later when I list all files, for those that are images, I can generate previews based on mime type.
So PHP.NET suggest to use exif_imagetype
function to check if file is an image. In case of success an integer will be returned indicating one of 18 constants.
The check I do is against an UploadedFile
so my method is quite simple. As this is code review. Would you optimize my code to be even better? Thanks for any hints.
/**
* @link https://www.php.net/manual/en/function.exif-imagetype.php
*
* @param UploadedFile $file
* @return bool
*/
private function isFileImage(UploadedFile $file): bool
{
$result = exif_imagetype($file->getRealPath());
return is_int($result) && $result >= 1 && $result <= 18;
}