PHP extension EXIF (Exchangeable image file format) ช่วยทำงานกับ metadata จากภาพถ่ายจาก digital devices เช่น digital cameras, cell phones ขึ้นอยู่กับ image file format สามารถดึง embedded thumbnails ได้
ฟังก์ชัน exif_imagetype() ใช้กำหนด type ของ image ฟังก์ชันอ่าน bytes แรกของ image ที่กำหนดและตรวจสอบ signature สามารถใช้เพื่อหลีกเลี่ยงการเรียก EXIF functions อื่นกับ unsupported file types หรือร่วมกับ $_SERVER['HTTP_ACCEPT'] ตรวจสอบว่า viewer เห็นภาพเฉพาะใน browser ได้หรือไม่
Syntax
integer exif_imagetype($str filename)
Parameters
exif_imagetype() รับ parameter เดียว $filename เก็บชื่อไฟล์ภาพ
Return Values
เมื่อเจอ signature ที่ถูกต้อง exif_imagetype() คืนค่า constant ที่เหมาะสม มิฉะนั้นคืนค่า False
List of Imagetype Constants
| Value | Constant | Value | Constant |
|---|---|---|---|
| 1 | IMAGETYPE_GIF | 10 | IMAGETYPE_JP2 |
| 2 | IMAGETYPE_JPEG | 11 | IMAGETYPE_JPX |
| 3 | IMAGETYPE_PNG | 12 | IMAGETYPE_JB2 |
| 4 | IMAGETYPE_SWF | 13 | IMAGETYPE_SWC |
| 5 | IMAGETYPE_PSD | 14 | IMAGETYPE_IFF |
| 6 | IMAGETYPE_BMP | 15 | IMAGETYPE_WBMP |
| 7 | IMAGETYPE_TIFF_II (intel byte order) | 16 | IMAGETYPE_XBM |
| 8 | IMAGETYPE_TIFF_MM (motorola byte order) | 17 | IMAGETYPE_ICO |
| 9 | IMAGETYPE_JPC | 18 | IMAGETYPE_WEBP |
Example 1
<?php
// Load an image from local drive
$filetype = exif_imagetype('C:\xampp\htdocs\test\office.jpg');
//Output
echo "The file type is: ", $filetype;
?>
ใช้ภาพ jpg ดังนี้:

Output
The file type is: 2
Example 2
ลองกับภาพ png:
<?php
$filetype = exif_imagetype('C:\xampp\htdocs\test\img46.png');
echo "The file type is: ", $filetype;
?>
ใช้ภาพ png:

Output
The file type is: 3