(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
exif_read_data — 從一個(gè)圖片文件中讀取 EXIF 頭信息
$file
,$required_sections
= null
,$as_arrays
= false
,$read_thumbnail
= false
exif_read_data() 函數從一個(gè)圖片文件中讀取 EXIF 頭信息。這樣就可以讀取數碼相機產(chǎn)生的元數據。
EXIF 頭信息往往存在于數碼相機生成的 JPEG/TIFF 圖片中,但不幸的是每個(gè)數碼相機制造商的標記都不同,因此(編寫(xiě)代碼時(shí))不能依賴(lài)于某個(gè)特定的 Exif 頭信息。
Height
和 Width
是用和 getimagesize()
一樣的方法計算的,因此它們的值不能是任何返回的頭信息的部分。此外
html
是一個(gè)可以用于普通的 HTML 中的 height/width 的文本字符串。
當一個(gè) Exif 頭信息包含有一個(gè) Copyright 時(shí)注意它本身可以包含兩個(gè)值。解決方案和
Exif 2.10 標準不一致,COMPUTED
區段會(huì )同時(shí)返回
Copyright.Photographer
和
Copyright.Editor
,但是 IFD0
區段則包含有一個(gè)字節數組用 NULL 字符分隔開(kāi)兩個(gè)項目?;蛘咧挥械谝豁椚绻麛祿?lèi)型錯誤的話(huà)(Exif
的正常行為)。COMPUTED
也會(huì )包含
Copyright
,要么是原始的版權字符串,要么是逗號分隔的攝像與編輯的版權信息。
UserComment
標記和 Copyright
有同樣的問(wèn)題。它也可以存儲兩個(gè)值,第一個(gè)是使用的編碼方式,第二個(gè)是其值本身。如果這樣則
IFD
區段僅包含編碼方式或者一個(gè)字節數組。COMPUTED
區段將存儲兩個(gè)值到 UserCommentEncoding
和
UserComment
。UserComment
在兩種情況下都可用因此應該優(yōu)先使用它而不是 IFD0
區段中的該值。
exif_read_data() 還會(huì )根據 EXIF 規范(? http://exif.org/Exif2-2.PDF,第 20 頁(yè))來(lái)驗證 EXIF 數據。
file
圖片文件的位置。這可以是一個(gè)文件路徑(和往常一樣,流封裝也是支持的), 或者是一個(gè)流式資源(stream resource)。
required_sections
是需要存在于文件中的逗號分隔的區段列表用來(lái)產(chǎn)生結果數組。如果未找到所請求的區段則返回值為
false
。
FILE | FileName, FileSize, FileDateTime, SectionsFound |
COMPUTED |
html,Width,Height,IsColor,可能有更多其它的。Height
和 Width 是用和 getimagesize()
一樣的方法計算的,因此它們的值不能是任何返回的頭信息的部分。此外
html 是一個(gè)可以用于普通的 HTML 中的 height/width 的文本字符串。
|
ANY_TAG | 任何包含有標記的信息,例如 IFD0 ,EXIF ,... |
IFD0 | 所有 IFD0 的標記數據。在標準的圖像文件中這包含了圖像大小及其它。 |
THUMBNAIL |
如果有第二個(gè) IFD ,文件應該包含有縮略圖。所有有關(guān)嵌入縮略圖的標記信息都存儲在本區。
|
COMMENT | JPEG 圖像的注釋頭信息。 |
EXIF |
EXIF 區段是 IFD0 的子區,包含有圖像的更多詳細信息。大多數內容都是數碼相機相關(guān)的。
|
as_arrays
指定了是否每個(gè)區段都成為一個(gè)數組。required_sections
COMPUTED
,THUMBNAIL
和COMMENT
區段總是成為數組,因為它們里面包含的名字和其它區段沖突。
read_thumbnail
當設定為 true
時(shí),讀取縮略圖本身。否則只讀取標記數據。
返回一個(gè)關(guān)聯(lián)數組,鍵名是頭信息名,值為與其相應的值。如果沒(méi)有可供返回的數據,exif_read_data()
將返回 false
。
當遇到不支持的標簽,或其他潛在的錯誤情況時(shí),將拋出E_WARNING
與E_NOTICE
等級的錯誤,但這個(gè)函數依然會(huì )嘗試去讀取所有可理解的信息。
版本 | 說(shuō)明 |
---|---|
8.0.0 |
required_sections 現在可以為空。
|
7.2.0 |
file 參數現在起支持本地文件和流式資源。
|
7.2.0 |
新增了以下 EXIF 格式的支持:
|
示例 #1 exif_read_data() 例子
<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?>
第一個(gè)調用失敗了,因為圖像沒(méi)有頭信息。
以上例程的輸出類(lèi)似于:
test1.jpg: No header data found. test2.jpg: FILE.FileName: test2.jpg FILE.FileDateTime: 1017666176 FILE.FileSize: 1240 FILE.FileType: 2 FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, COMMENT COMPUTED.html: width="1" height="1" COMPUTED.Height: 1 COMPUTED.Width: 1 COMPUTED.IsColor: 1 COMPUTED.ByteOrderMotorola: 1 COMPUTED.UserComment: Exif test image. COMPUTED.UserCommentEncoding: ASCII COMPUTED.Copyright: Photo (c) M.Boerger, Edited by M.Boerger. COMPUTED.Copyright.Photographer: Photo (c) M.Boerger COMPUTED.Copyright.Editor: Edited by M.Boerger. IFD0.Copyright: Photo (c) M.Boerger IFD0.UserComment: ASCII THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.JPEGInterchangeFormatLength: 523 COMMENT.0: Comment #1. COMMENT.1: Comment #2. COMMENT.2: Comment #3end THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.Thumbnail.Height: 1 THUMBNAIL.Thumbnail.Height: 1
示例 #2 exif_read_data() with streams available as of PHP 7.2.0
<?php
// Open a the file, this should be in binary mode
$fp = fopen('/path/to/image.jpg', 'rb');
if (!$fp) {
echo 'Error: Unable to open image for reading';
exit;
}
// Attempt to read the exif headers
$headers = exif_read_data($fp);
if (!$headers) {
echo 'Error: Unable to read exif headers';
exit;
}
// Print the 'COMPUTED' headers
echo 'EXIF Headers:' . PHP_EOL;
foreach ($headers['COMPUTED'] as $header => $value) {
printf(' %s => %s%s', $header, $value, PHP_EOL);
}
?>
以上例程的輸出類(lèi)似于:
EXIF Headers: Height => 576 Width => 1024 IsColor => 1 ByteOrderMotorola => 0 ApertureFNumber => f/5.6 UserComment => UserCommentEncoding => UNDEFINED Copyright => Denis Thumbnail.FileType => 2 Thumbnail.MimeType => image/jpeg
注意:
If mbstring is enabled, exif will attempt to process the unicode and pick a charset as specified by exif.decode_unicode_motorola and exif.decode_unicode_intel. The exif extension will not attempt to figure out the encoding on its own, and it is up to the user to properly specify the encoding for which to use for decoding by setting one of these two ini directives prior to calling exif_read_data().
注意:
If the
file
is used to pass a stream to this function, then the stream must be seekable. Note that the file pointer position is not changed after this function returns.