2012-05-26 157 views
0

我有以下代碼以從圖像中提取和顯示EXIF信息。GPS座標轉換 - PHP

<? 
    // +++++++++++++++++++++++++ // 
    // +++++++ GPS EXIF +++++++ // 
    // +++++++++++++++++++++++++ // 

    // --- Change the 'image1.jpg' value below to the path of your file. --- // 
    $Image = 'image1.jpg'; 

    // --- The EXIF read command --- // 
    $ReadEXIFData = read_exif_data($Image, 0, true); 


    // +++++++++++++++++ // 
    // +++ FILE INFO +++ // 
    // +++++++++++++++++ // 
    // --- Get the filename --- // 
    $Filename = $ReadEXIFData['FILE']['FileName']; 
    // --- Get the file size --- // 
    $FileSize = $ReadEXIFData['FILE']['FileSize']; 
    // --- Converts to user-friendly kb, mb and gb sizes --- // 
    if ($FileSize >= 1024) { $ImageFileSize = number_format($FileSize/1024, 2).'kb'; } 
    elseif ($FileSize >= 1048576) { $ImageFileSize = number_format($FileSize/1048576, 2).'mb'; } 
    elseif ($FileSize >= 1073741824) { $ImageFileSize = number_format($FileSize/1073741824, 2).'gb'; } 
    // --- Get the file height --- // 
    $FileHeight = $ReadEXIFData['COMPUTED']['Height'].'px'; 
    // --- Get the file height --- // 
    $FileWidth = $ReadEXIFData['COMPUTED']['Width'].'px'; 



    // +++++++++++++++++++++++ // 
    // +++ GPS COORDINATES +++ // 
    // +++++++++++++++++++++++ // 
    // --- Get GPS hemisphire --- // 
    $GPSHemisphire = $ReadEXIFData["GPS"]["GPSLatitudeRef"]; 

    // --- Get GPS degrees latitude --- // 
    $GPSDegreesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][0]); 
    // --- Get GPS minutes latitude --- // 
    $GPSMinutesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][1]); 
    // --- Get GPS seconds latitude --- // 
    $GPSSecondsLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][2]); 
    // --- Get GPS Hemisphere latitude --- // 
    $GPSSecondsLat2 = ($ReadEXIFData["GPS"]["GPSLatitudeRef"]); 
    // --- Decimal Latitude goes here --- // 


    // --- Get GPS degrees longitude --- // 
    $GPSDegreesLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][0]); 
    // --- Get GPS minutes longitude --- // 
    $GPSMinutesLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][1]); 
    // --- Get GPS seconds longitude --- // 
    $GPSSecondsLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][2]); 
    // --- Get GPS Hemisphere longitude --- // 
    $GPSSecondsLon2 = ($ReadEXIFData["GPS"]["GPSLongitudeRef"]); 
    // --- Decimal Longitude goes here --- // 


    // --- Get GPS altitude --- // 
    $GPSAltitude = $ReadEXIFData["GPS"]["GPSAltitude"][0]; 


    ?> 

的信息,然後在HTML頁面打印成可以在這裏http://ukf.com/easyexif/easyexif3.php

看到的問題是,返回的GPS座標爲度/分/秒。

將它們轉換爲十進制我有此腳本:

function toDecimal($deg, $min, $sec, $hemi) { 
    $d = $deg + $min/60 + $sec/3600; 
    return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d; 
    } 

    function divide($a) { 
    $e = explode('/', $a); 
    if (!$e[0] || !$e[1]) { 
     return 0; 
    } else { 
     return $e[0]/$e[1]; 
    } 
    } 

    function getGPS() { 
    global $exif; 
    if ($exif) { $lat = $exif['GPS']['GPSLatitude']; 
    $log = $exif['GPS']['GPSLongitude']; 
     if (!$lat || !$log) return null; 
     $lat_degrees = divide($lat[0]); 
     $lat_minutes = divide($lat[1]); 
     $lat_seconds = divide($lat[2]); 
     $lat_hemi = $exif['GPS']['GPSLatitudeRef']; 
     $log_degrees = divide($log[0]); 
     $log_minutes = divide($log[1]); 
     $log_seconds = divide($log[2]); 
     $log_hemi = $exif['GPS']['GPSLongitudeRef']; 
     $lat_decimal = toDecimal($lat_degrees, $lat_minutes, $lat_seconds, $lat_hemi); 
     $log_decimal = toDecimal($log_degrees, $log_minutes, $log_seconds, $log_hemi); 
     return array($lat_decimal, $log_decimal); 
    } else{ 
     return null; 
    } 
    } 

我需要的是十進制Logitude和緯度的形式返回

$GPSDECLON 
    $GPSDECLAT 

,所以我可以包括它們與上述信息在我的HTML頁面。

我的PHP知識目前是如此有限我不知道如何去包括轉換到我現有的腳本。

任何人都可以幫忙嗎?如果是的話,它會大大降低。

+0

如果我來到新的這個劇本我猜想,'$ GPSSecondsLat2'和'$ GPSSecondsLon2'可能是你是什麼東西尋找 - 以小數秒爲單位的經緯度。順便說一句,似乎'$ GPSHemisphire'的分配是不正確的。 – SEngstrom

+0

對不起 - 應該提到'Decimal Latitude goes here'和'Decimal Longitude goes here'是添加轉換腳本時要計算的新值的標題。 $ GPSSecondsLat2和$ GPSSecondsLon2調用半球值(即N/S和E/W)在網頁上看看,你會明白我的意思。 – pewe

回答

0

有什麼不妥,用秒呈現在網頁上 - 他們必須在範圍[0,60 [

這就是說,該轉換是它在PHP代碼中的第一個函數說你發佈:

function toDecimal($deg, $min, $sec, $hemi) { 
$d = $deg + $min/60 + $sec/3600; 
return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d; 
} 

也就是說,有60分鐘到一定程度,60秒到一分鐘。如果你在南半球,則翻轉緯度,如果你在「西半球」翻轉經度值,這些只是兩次測量的基本標誌。

所以......

$ GPSDECLON = toDecimal($ GPSDegreesLon,$ GPSMinutesLon,$ GPSSecondsLon,$ EWHemisphere)

其中$ EWHemisphere是 「E」 或 「W」 這取決於你如何半球變量編碼它。 然後它是一個類似的緯度線,除非你需要知道它是否爲符號的「N」或「S」。

希望幫助和我道歉,如果我在這裏失去了你的問題......

+0

在原始腳本(intval)中存在一段代碼,它剝離了刪除其中一部分的值。這就是爲什麼價值觀就像你看到的那樣。我已經刪除了'intval',現在您將在網頁上看到該值作爲分數返回。我發佈的代碼的目的是分離分數並取消分數以返回小數點的值,然後將miutes和seconds轉換爲度數的小數點 - 例如,緯度的結果應爲51.48571236。該代碼的作品,我只是不知道如何將其插入原始腳本。 – pewe