整理照片,取出 GPS 座標

edited 十月 2013 in 原創軟體分享區
很多智慧型手機拍的照片都帶有 GPS 座標,也慢慢越來越多相機支援,只是現在大多還是靠特定軟體或網站服務才有辦法瀏覽。

首先是檔名,我都是在 Linux 環境作業,照片的部份我會透過 phatch photo batch processor 將它依據日期、時間重新命名為 "日期資料夾/日期+時間.jpg" ,像是 20111002/20111002_121338.jpg 這樣的形式,影片的部份就會借助一些 linux 指令,透過簡單的 PHP 程式整理
<?php
$files = explode("\n", `find . -type f`);
foreach($files AS $file) {
    $file = trim($file);
    if(!empty($file) && $file != './rename.php') {
        $pathParts = pathinfo($file);
        $mTime = filemtime($file);
		$info = `mediainfo {$file}`;
		$pos1 = strpos($info, 'Encoded date');
		if(false !== $pos1) {
			$pos1 = strpos($info, 'UTC', $pos1) + 4;
			$date = substr($info, $pos1, 19);
			$mTime = strtotime($date);
		}
        if(!is_dir(date('Ymd', $mTime))) {
            mkdir(date('Ymd', $mTime), 0777);
        }
        $newTarget = date('Ymd/Ymd_His', $mTime) . '.' . $pathParts['extension'];
        rename($file, $newTarget);
    }
}

$folders = explode("\n", `find . -type d`);
foreach($folders AS $folder) {
	if($folder == '.' || $folder == '..') continue;
	$folder = trim($folder);
	$content = `ls -1 {$folder}`;
	if(empty($content)) {
		rmdir($folder);
	}
}

依據檔名整理完之後,我又想要把 GPS 座標挖出來,所以再跑這一個程式
<?php
/*
methods coming from http://stackoverflow.com/questions/2526304
*/
function getGps($exifCoord, $hemi) {

    $degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
    $minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
    $seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;

    $flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;

    return $flip * ($degrees + $minutes / 60 + $seconds / 3600);
}

function gps2Num($coordPart) {

    $parts = explode('/', $coordPart);

    if (count($parts) <= 0)
        return 0;

    if (count($parts) == 1)
        return $parts[0];

    return floatval($parts[0]) / floatval($parts[1]);
}

$targetPath = '/home/kiang/Desktop/pic/*/*';
$exts = array('jpg', 'JPG');
$keys = array('GPSLongitude', 'GPSLongitudeRef', 'GPSLatitude', 'GPSLatitudeRef');
$fileStack = array();
foreach (glob($targetPath) AS $file) {
    if (in_array(substr($file, -3), $exts)) {
        $exif = exif_read_data($file);
        $keyCheck = true;
        foreach ($keys AS $key) {
            if ($keyCheck && empty($exif[$key])) {
                $keyCheck = false;
            }
        }
        if ($keyCheck) {
            $lon = getGps($exif["GPSLongitude"], $exif['GPSLongitudeRef']);
            $lat = getGps($exif["GPSLatitude"], $exif['GPSLatitudeRef']);
            if (!empty($lon) && !empty($lat)) {
                $time = explode('_', substr(basename($file), 0, -4));
                if (!isset($fileStack[$time[0]])) {
                    $fileStack[$time[0]] = array();
                }
                $fileStack[$time[0]][$time[1]] = array(
                    'file' => $file,
                    'lat' => $lat,
                    'lng' => $lon,
                );
            }
        }
    }
}
ksort($fileStack);
foreach($fileStack AS $day => $pics) {
    $targetFile = 'gps/' . substr($day, 0, 6) . '.html';
    $fh = fopen($targetFile, 'a+');
    $dayChunks = str_split($day, 2);
    $dayTime = mktime(1, 0, 0, $dayChunks[2], $dayChunks[3], $dayChunks[0] . $dayChunks[1]);
    fwrite($fh, '<h1>' . date('Y-m-d(D)', $dayTime) . '</h1>');
    foreach($pics AS $time => $file) {
        $imagePath = str_replace('/home/kiang/Desktop/', '../', $file['file']);
        fwrite($fh, "<br /><img src='{$imagePath}' width='400' /><span>{$file['lat']},{$file['lng']}</span>");
    }
    fwrite($fh, '<hr />');
    fclose($fh);
}

執行完成後就會依據月份產生一些索引網頁,需要的時候就把座標資訊貼到 google map 去找對應的位置

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=7192
Sign In or Register to comment.