phpでピクチャ完全Exif情報クラスを取得ピクチャ詳細完全情報クラスを取得

45224 ワード

<?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>';  

  

?>