Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – ฟังก์ชัน exif_imagetype()

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

ValueConstantValueConstant
1IMAGETYPE_GIF10IMAGETYPE_JP2
2IMAGETYPE_JPEG11IMAGETYPE_JPX
3IMAGETYPE_PNG12IMAGETYPE_JB2
4IMAGETYPE_SWF13IMAGETYPE_SWC
5IMAGETYPE_PSD14IMAGETYPE_IFF
6IMAGETYPE_BMP15IMAGETYPE_WBMP
7IMAGETYPE_TIFF_II (intel byte order)16IMAGETYPE_XBM
8IMAGETYPE_TIFF_MM (motorola byte order)17IMAGETYPE_ICO
9IMAGETYPE_JPC18IMAGETYPE_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 ดังนี้:

PHP – ฟังก์ชัน exif_imagetype()

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:

PHP – ฟังก์ชัน exif_imagetype()

Output

The file type is: 3