這個是最近寫的一個php獲取圖片Exif類,功能比較全面,也把所有的Exif都翻譯了,執行效率也還可以。
唉,這次懶地寫英文版了,以后有時間把英文版也做下。同樣,不設置隱藏,也不需金錢。
因為這里不能貼鏈接,我博客里有個測試網頁,大家如果有興趣,可以去看看tonylevid.com
分兩大模式:一個是已經轉換數據的(如width:500px)用於顯示;一個是未轉換數據的如(width:500)用於用戶自己自定義計算。
內置3種獲取模式,用戶可以自定義數組來獲取。
注意:需在php.ini中開啟mbstring和exif模塊
功能如下:
@可以獲取已經轉換后的值,也可以獲取未轉換的值用於自己自定義計算
@內置3種獲取模式,分別為簡要信息、常用信息和全部信息
@用戶可以自定義獲取信息
具體可以查看類中的注釋
<?php /** * @Author: TonyLevid * @Copyright: TonyLevid.com * @Name: Image Exif Class * @Version: 0.0.1 * 歡迎各位測試,如有BUG,請到網站留言 * I'm pleased if you are willing to test my Image Exif class,if bug exists,you can leave a message. **/ //error_reporting(0); 不想讓用戶看到錯誤如果存在bug header("content-type:text/html;charset=utf-8"); //如果在html頁面已經設置編碼,請刪除此行 class imgExif{ public $imgPath; public $unitFlag; public $imgInfoAll; public $imgInfoAllCN; public $imgInfoCommon; public $imgInfoBrief; public $imgInfoAllCNUnit; /*構造函數,檢測exif和mbstring模塊是否開啟*/ function __construct(){ extension_loaded('exif')&&extension_loaded('mbstring') or die('exif module was not loaded,please check it in php.ini<br>NOTICE:On Windows,php_mbstring.dll must be before php_exif.dll'); } /*獲取圖片格式,返回圖片格式信息 * 如果只獲取圖片格式信息,建議采用此方法 * * @param $imgPath(必填,字符串),圖片路徑,不可為url。 * @param $MimeOrExifOrExtension(可選,字符串),獲取圖片格式為Mime類型或Exif類型或圖片類型文件后綴。 * 如果為字符串'Mime',則獲取Mime圖片類型。 * 如果為字符串'Exif',則獲取Exif圖片類型。 * 如果為字符串'Extension',則獲取圖片類型的文件后綴。 * 如果填寫參數異常或缺省,則默認獲取Mime圖片類型。 */ function getImgtype($imgPath,$MimeOrExifOrExtension = null){ $exifImgtype = array( 'IMAGETYPE_GIF' => 1, 'IMAGETYPE_JPEG' => 2, 'IMAGETYPE_PNG' => 3, 'IMAGETYPE_SWF' => 4, 'IMAGETYPE_PSD' => 5, 'IMAGETYPE_BMP' => 6, 'IMAGETYPE_TIFF_II' => 7, //(Intel 字節順序) 'IMAGETYPE_TIFF_MM' => 8, //(Motorola 字節順序) 'IMAGETYPE_JPC' => 9, 'IMAGETYPE_JP2' => 10, 'IMAGETYPE_JPX' => 11, 'IMAGETYPE_JB2' => 12, 'IMAGETYPE_SWC' => 13, 'IMAGETYPE_IFF' => 14, 'IMAGETYPE_WBMP' => 15, 'IMAGETYPE_XBM' => 16 ); $exifType = array_search(exif_imagetype($imgPath),$exifImgtype); $mimeType = image_type_to_mime_type(exif_imagetype($imgPath)); $extension = substr(image_type_to_extension(exif_imagetype($imgPath)),1); if($MimeOrExifOrExtension){ if($MimeOrExifOrExtension === 'Mime'){ return $mimeType; } elseif($MimeOrExifOrExtension === 'Exif'){ return $exifType; } elseif($MimeOrExifOrExtension === 'Extension'){ return $extension; } else{ return $mimeType; } } else{ return $mimeType; } } /*處理Exif信息*/ function imgInfo(){ $imgPath = $this->imgPath; $imgInfoAll = exif_read_data($imgPath,0,1); foreach($imgInfoAll as $section => $arrValue){ foreach($arrValue as $key => $value){ $infoAllKey[] = $key; $infoAllValue[] = $value; } } $infoAll = array_combine($infoAllKey,$infoAllValue); $translate = array( 'FileName'=>'文件名', 'FileDateTime' => '文件修改時間', 'FileSize' => '文件大小', 'FileType' => 'Exif文件類型', 'MimeType' => 'Mime文件類型', 'SectionsFound' => '找到Sections', 'html' => 'html中圖片寬高', 'Height' => '圖片高度', 'Width' => '圖片寬度', 'IsColor' => '是否彩色', 'ByteOrderMotorola' => '是否為Motorola字節順序', 'ApertureFNumber' => '光圈數', 'Comments' => '作者注釋', 'Author' => '作者', 'UserComment' => '用戶注釋', 'UserCommentEncoding' => '用戶注釋編碼', 'Thumbnail.FileType' => '縮略圖Exif文件類型', 'Thumbnail.MimeType' => '縮略圖Mime文件類型', 'Make' => '制造商', 'Model' => '型號', 'Orientation' => '方向', 'XResolution' => '水平分辨率', 'YResolution' => '垂直分辨率', 'ResolutionUnit' => '分辨率單位', 'Software' => '創建軟件', 'DateTime' => '最后修改時間', 'YCbCrPositioning' => 'YCbCr位置控制', 'Exif_IFD_Pointer' => 'Exif圖像IFD的指針', 'Compression' => '壓縮方式', 'JPEGInterchangeFormat' => 'JPEG SOI偏移', 'JPEGInterchangeFormatLength' => 'JPEG數據字節', 'ExposureTime' => '曝光時間', 'FNumber' => '焦距比數', 'ExposureProgram' => '曝光程序', 'ISOSpeedRatings' => 'ISO感光度', 'ExifVersion' => 'Exif版本', 'DateTimeOriginal' => '拍攝時間', 'DateTimeDigitized' => '數字化時間', 'ComponentsConfiguration' => '分量配置', 'CompressedBitsPerPixel' => '圖像壓縮率', 'ExposureBiasValue' => '曝光補償', 'MaxApertureValue' => '最大光圈值', 'MeteringMode' => '測光模式', 'LightSource' => '光源', 'Flash' => '閃光燈', 'FocalLength' => '焦距', 'SubSecTime' => '亞秒時間', 'SubSecTimeOriginal' => '亞秒級拍攝時間', 'SubSecTimeDigitized' => '亞秒級數字化時間', 'FlashPixVersion' => 'FlashPix版本', 'ColorSpace' => '色彩空間', 'ExifImageWidth' => 'Exif圖片寬度', 'ExifImageLength' => 'EXif圖片高度', 'InteroperabilityOffset' => 'IFD格式數據偏移量', 'SensingMethod' => '彩色區域傳感器類型', 'FileSource' => '圖片像源', 'SceneType' => '場景類型', 'CFAPattern' => '濾波陣列圖案', 'CustomRendered' => '自定義圖像處理', 'ExposureMode' => '曝光模式', 'WhiteBalance' => '白平衡', 'DigitalZoomRatio' => '數位變焦倍率', 'FocalLengthIn35mmFilm' => '等價35mm焦距', 'SceneCaptureType' => '取景模式', 'GainControl' => '增益控制', 'Contrast' => '對比度', 'Saturation' => '飽和度', 'Sharpness' => '清晰度', 'SubjectDistanceRange' => '對焦距離', 'InterOperabilityIndex' => 'InterOperability指數', 'InterOperabilityVersion' => 'InterOperability版本' ); @$translate_unit = array( '文件名' => $infoAll['FileName'], '文件修改時間' => date('Y:m:d H:i:s',$infoAll['FileDateTime']), '文件大小' => round($infoAll['FileSize']/1024) . 'kb', 'Exif文件類型' => $this->getImgtype($imgPath,'Exif'), 'Mime文件類型' => $infoAll['MimeType'], '找到Sections' => $infoAll['SectionsFound'], 'html中圖片寬高' => $infoAll['html'], '圖片高度' => $infoAll['Height'] . 'px', '圖片寬度' => $infoAll['Width'] . 'px', '是否彩色' => $infoAll['IsColor'] == 1 ? '是' : '否', '是否為Motorola字節順序' => $infoAll['ByteOrderMotorola'] == 1 ? '是' : '否', '光圈數' => $infoAll['ApertureFNumber'], '作者注釋' => $infoAll['Comments'], '作者' => $infoAll['Author'], '用戶注釋' => $infoAll['UserComment'], '用戶注釋編碼' => $infoAll['UserCommentEncoding'], '縮略圖Exif文件類型' => $this->getImgtype($imgPath,'Exif'), '縮略圖Mime文件類型' => $infoAll['Thumbnail.MimeType'], '制造商' => $infoAll['Make'], '型號' => $infoAll['Model'], '方向' => array_search($infoAll['Orientation'],array( 'top left side' => 1, 'top right side' => 2, 'bottom right side' => 3, 'bottom left side' => 4, 'left side top' => 5, 'right side top' => 6, 'right side bottom' => 7, 'left side bottom' => 8 )), '水平分辨率' => $infoAll['XResolution'], '垂直分辨率' => $infoAll['YResolution'], '分辨率單位' => array_search($infoAll['ResolutionUnit'],array( '無單位' => 1, '英寸' => 2, '厘米' => 3 )), '創建軟件' => $infoAll['Software'], '最后修改時間' => $infoAll['DateTime'], 'YCbCr位置控制' => $infoAll['YCbCrPositioning'] == 1 ? '像素陣列的中心' : '基准點', 'Exif圖像IFD的指針' => $infoAll['Exif_IFD_Pointer'], '壓縮方式' => $infoAll['Compression'] == 6 ? 'jpeg壓縮' : '無壓縮', 'JPEG SOI偏移' => $infoAll['JPEGInterchangeFormat'], 'JPEG數據字節' => $infoAll['JPEGInterchangeFormatLength'], '曝光時間' => $infoAll['ExposureTime'] . '秒', '焦距比數' => $infoAll['FNumber'], '曝光程序' => array_search($infoAll['ExposureProgram'],array( '手動控制' => 1, '程序控制' => 2, '光圈優先' => 3, '快門優先' => 4, '景深優先' => 5, '運動模式' => 6, '肖像模式' => 7, '風景模式' => 8 )), 'ISO感光度' => $infoAll['ISOSpeedRatings'], 'Exif版本' => $infoAll['ExifVersion'], '拍攝時間' => $infoAll['DateTimeOriginal'], '數字化時間' => $infoAll['DateTimeDigitized'], '分量配置' => $infoAll['ComponentsConfiguration'], '圖像壓縮率' => $infoAll['CompressedBitsPerPixel'], '曝光補償' => $infoAll['ExposureBiasValue'] . '電子伏特', '最大光圈值' => $infoAll['MaxApertureValue'], '測光模式' => array_search($infoAll['MeteringMode'],array( '未知' => 0, '平均' => 1, '中央重點平均測光' => 2, '點測' => 3, '分區' => 4, '評估' => 5, '局部' => 6, '其他' => 255 )), '光源' => array_search($infoAll['LightSource'],array( '未知' => 0, '日光燈' => 1, '熒光燈' => 2, '鎢絲燈' => 3, '閃光燈' => 10, '標准燈光A' => 17, '標准燈光B' => 18, '標准燈光C' => 19, 'D55' => 20, 'D65' => 21, 'D75' => 22, '其他' => 255, )), '閃光燈' => array_search($infoAll['Flash'],array( '閃光燈未閃光' => 0, '閃光燈已閃光' => 1, '閃光燈已閃光但頻閃觀測器未檢測到返回光源' => 5, '閃光燈已閃光,頻閃觀測器檢測到返回光源' => 7 )), '焦距' => $infoAll['FocalLength'] . '毫米', '亞秒時間' => $infoAll['SubSecTime'], '亞秒級拍攝時間' => $infoAll['SubSecTimeOriginal'], '亞秒級數字化時間' => $infoAll['SubSecTimeDigitized'], 'FlashPix版本' => $infoAll['FlashPixVersion'], '色彩空間' => $infoAll['ColorSpace'] == 1 ? 'sRGB' : 'Uncalibrated', 'Exif圖片寬度' => $infoAll['ExifImageWidth'] . 'px', 'EXif圖片高度' => $infoAll['ExifImageLength'] . 'px', 'IFD格式數據偏移量' => $infoAll['InteroperabilityOffset'], '彩色區域傳感器類型' => $infoAll['SensingMethod'] == 2 ? '單色區傳感器' : '其他', '圖片像源' => $infoAll['FileSource'] == '0x03' ? '數碼相機' : '其他', '場景類型' => $infoAll['SceneType'] == '0x01' ? '直接拍攝' : '其他', '濾波陣列圖案' => $infoAll['CFAPattern'], '自定義圖像處理' => $infoAll['CustomRendered'], '曝光模式' => $infoAll['CustomRendered'] == 1 ? '手動' : '自動', '白平衡' => $infoAll['WhiteBalance'] == 1 ? '手動' : '自動', '數位變焦倍率' => $infoAll['DigitalZoomRatio'], '等價35mm焦距' => $infoAll['FocalLengthIn35mmFilm'] . '毫米', '取景模式' => array_search($infoAll['SceneCaptureType'],array( '自動' => 0, '肖像場景' => 1, '景觀場景' => 2, '運動場景' => 3, '夜景' => 4, '自動曝光' => 5, '光圈優先自動曝光' => 256, '快門優先自動曝光' => 512, '手動曝光' => 768, )), '增益控制' => $infoAll['GainControl'], '對比度' => array_search($infoAll['Contrast'],array( '低' => -1, '普通' => 0, '高' => 1 )), '飽和度' => array_search($infoAll['Saturation'],array( '低' => -1, '普通' => 0, '高' => 1 )), '清晰度' => array_search($infoAll['Sharpness'],array( '低' => -1, '普通' => 0, '高' => 1 )), '對焦距離' => array_search($infoAll['SubjectDistanceRange'],array( '未知' => 0, '微距' => 1, '近景' => 2, '遠景' => 3 )), 'InterOperability指數' => $infoAll['InterOperabilityIndex'], 'InterOperability版本' => $infoAll['InterOperabilityVersion'] ); $infoAllCNKey = array_keys($translate); $infoAllCNName = array_values($translate); foreach($infoAllCNKey as $value){ @$infoAllCNValue[] = $infoAll[$value]; } $infoAllCNUnit = array_combine($infoAllCNName,array_values($translate_unit)); $infoAllCN = array_combine($infoAllCNName,$infoAllCNValue); $infoCommon = array( $translate['FileName'] => $infoAll['FileName'], $translate['DateTimeOriginal'] => $infoAll['DateTimeOriginal'], $translate['MimeType'] => $infoAll['MimeType'], $translate['Width'] => $infoAll['Width'], $translate['Height'] => $infoAll['Height'], $translate['Comments'] => $infoAll['Comments'], $translate['Author'] => $infoAll['Author'], $translate['Make'] => $infoAll['Make'], $translate['Model'] => $infoAll['Model'], $translate['CompressedBitsPerPixel'] => $infoAll['CompressedBitsPerPixel'], $translate['ExposureBiasValue'] => $infoAll['ExposureBiasValue'], $translate['MaxApertureValue'] => $infoAll['MaxApertureValue'], $translate['MeteringMode'] => $infoAll['MeteringMode'], $translate['LightSource'] => $infoAll['LightSource'], $translate['Flash'] => $infoAll['Flash'], $translate['FocalLength'] => $infoAll['FocalLength'], $translate['SceneType'] => $infoAll['SceneType'], $translate['CFAPattern'] => $infoAll['CFAPattern'], $translate['CustomRendered'] => $infoAll['CustomRendered'], $translate['ExposureMode'] => $infoAll['ExposureMode'], $translate['WhiteBalance'] => $infoAll['WhiteBalance'], $translate['DigitalZoomRatio'] => $infoAll['DigitalZoomRatio'], $translate['FocalLengthIn35mmFilm'] => $infoAll['FocalLengthIn35mmFilm'], $translate['SceneCaptureType'] => $infoAll['SceneCaptureType'], $translate['GainControl'] => $infoAll['GainControl'], $translate['Contrast'] => $infoAll['Contrast'], $translate['Saturation'] => $infoAll['Saturation'], $translate['Sharpness'] => $infoAll['Sharpness'], $translate['SubjectDistanceRange'] => $infoAll['SubjectDistanceRange'], $translate['Software'] => $infoAll['Software'], $translate['DateTime'] => $infoAll['DateTime'], $translate['FileSize'] => $infoAll['FileSize'] ); foreach($infoCommon as $cKey => $cKalue){ $infoCommonUnitKeys[] = $cKey; $infoCommonUnitValues[] = $translate_unit[$cKey]; } $infoCommonUnit = array_combine($infoCommonUnitKeys,$infoCommonUnitValues); $infoBrief = array( $translate['FileName'] => $infoAll['FileName'], $translate['Width'] => $infoAll['Width'], $translate['Height'] => $infoAll['Height'], $translate['DateTimeOriginal'] => $infoAll['DateTimeOriginal'], $translate['Make'] => $infoAll['Make'], $translate['Model'] => $infoAll['Model'], $translate['MimeType'] => $infoAll['MimeType'] ); foreach($infoBrief as $bKey => $bValue){ $infoBriefUnitKeys[] = $bKey; $infoBriefUnitValues[] = $translate_unit[$bKey]; } $infoBriefUnit = array_combine($infoBriefUnitKeys,$infoBriefUnitValues); $this->imgInfoAll = $infoAll; $this->imgInfoAllCN = $infoAllCN; $this->imgInfoAllCNUnit = $infoAllCNUnit; $this->imgInfoCommon = $this->unitFlag ? $infoCommonUnit : $infoCommon; $this->imgInfoBrief = $this->unitFlag ? $infoBriefUnit : $infoBrief; } /*獲取圖片Exif信息,返回Exif信息一維數組 * * @param $imgPath(必填,字符串),圖片路徑,不可為url。 * @param $iChoice(可選,字符串或一維數組) * 此參數內置了三種模式: * 如果為字符串'All',則獲取所有信息; * 如果為字符串'Common',則獲取常用信息; * 如果為字符串'Brief',則獲取簡要信息。 * 用戶可以自定義數組獲取精確的信息,如array('圖片寬度','圖片高度'),則獲取圖片寬度和高度。 * 對於異常輸入或缺省,則自動獲取簡要信息。 * @param $showUnit(可選,字符串),只要不為null,則獲取已經轉換后的值,否則獲取未轉換后的值。 */ function getImgInfo($imgPath,$iChoice = null,$showUnit = null){ $this->imgPath = $imgPath; $this->unitFlag = $showUnit; $this->imgInfo(); $this->imgInfoAllCN = $showUnit ? $this->imgInfoAllCNUnit : $this->imgInfoAllCN; if($iChoice){ if(is_string($iChoice)){ if($iChoice === 'All'){ return $this->imgInfoAllCN; } elseif($iChoice === 'AllUnit'){ return $this->imgInfoAllCN; } elseif($iChoice === 'Common'){ return $this->imgInfoCommon; } elseif($iChoice === 'Brief'){ return $this->imgInfoBrief; } else{ return $this->imgInfoBrief; } } elseif(is_array($iChoice)){ foreach($iChoice as $value){ $arrCustomValue[] = $this->imgInfoAllCN[$value]; } $arrCustom = array_combine($iChoice,$arrCustomValue) or die('Ensure the array $iChoice values match $infoAll keys!'); return $arrCustom; } else{ return $this->imgInfoBrief; } } else{ return $this->imgInfoBrief; } } } //示例,同時檢測腳本執行時間 function exeTime(){ $micro = microtime(); list($usec,$sec) = explode(' ',$micro); return ($sec + $usec); } $start = exeTime(); $i = new imgExif(); //echo '<font color=\'blue\'>圖片格式:' . $i->getImgtype('12.jpg','Extension') . '<br><br></font>'; $arr = $i->getImgInfo('12.jpg','All','1'); foreach($arr as $key => $value){ echo $key . ': ' . $value . '<br>'; } $end = exeTime(); echo '<br><font color=\'red\'>腳本執行時間:' . ($end - $start) . '<br></font>'; ?>
轉載:http://www.itokit.com/2012/0722/74610.html