Check image type in PHP

Quick PHP tip: If you’re looking to determine the type of uploaded image you can use the following code:

$type = exif_imagetype($_FILES['uploaded_image']['tmp_name']);
if ($type == 1 || $type == 2 || $type == 3)
//image type is jpg, gif or png

The code above shows a snippet to check if the uploaded image was a jpg, gif or png image type. As stated on the PHP documentation, this will return the same value as getimagesize() but it’s much faster.

, , ,