diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-08-19 19:06:56 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-08-19 19:07:04 +0200 |
commit | c01e5c96c815c53695b7d80923a51cd235edd841 (patch) | |
tree | 0e904ffd71ea1df39b624dc02aec33bcb025afb2 /pictures | |
parent | f8de7e3ce331b80be37ede2f070f9ad92150581c (diff) | |
download | gpx-gallery-c01e5c96c815c53695b7d80923a51cd235edd841.tar.gz gpx-gallery-c01e5c96c815c53695b7d80923a51cd235edd841.tar.bz2 gpx-gallery-c01e5c96c815c53695b7d80923a51cd235edd841.tar.xz gpx-gallery-c01e5c96c815c53695b7d80923a51cd235edd841.zip |
wip
Diffstat (limited to 'pictures')
-rw-r--r-- | pictures/index.php | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/pictures/index.php b/pictures/index.php new file mode 100644 index 0000000..0bee830 --- /dev/null +++ b/pictures/index.php @@ -0,0 +1,128 @@ +<?php + +$hash = hash("md5", $_SERVER['PHP_SELF']); +$basedir = sys_get_temp_dir() . "/$hash"; + +if (!is_dir($basedir)) + mkdir($basedir); + +try { + function isValidFileName($filename) { + return preg_match("/^[_a-zA-Z0-9]*\.JPG$/i", $filename) == 1; + } + + function parseVal($s) { + $major = strstr($s, '/', true); + $minor = substr(strstr($s, '/'), 1); + return intval($major) / intval($minor); + } + + function parseDeg($val) { + $deg = parseVal($val[0]); + $min = parseVal($val[1]); + $sec = parseVal($val[2]); + return $deg + $min/60 + $sec / 3600; + } + + function processFile($filename) { + global $basedir; + $cacheFile = "$basedir/$filename.geojson"; + + if (!isValidFileName($filename)) { + throw new InvalidArgumentException("Invalid file name: $filename"); + } + + if (false && is_readable($cacheFile)) { + $s = file_get_contents($cacheFile); + if ($s !== false) { + return json_decode($s); + } + } + + $exif = exif_read_data($filename, 0, true); + if (!$exif) { + throw new InvalidArgumentException("Could not open file: $filename"); + } + + $json = ["type" => "Feature", "id" => $filename]; + + foreach ($exif as $key => $section) { + foreach ($section as $name => $val) { + switch("$key.$name") { + case "FILE.FileSize": $filesize_bytes = $val; break; + case "COMPUTED.Height": $height = $val; break; + case "COMPUTED.Width": $width = $val; break; + case "GPS.GPSLongitude": $lon = parseDeg($val); break; + case "GPS.GPSLatitude": $lat = parseDeg($val); break; + case "IFD0.Orientation": $orientation = $val; break; + case "EXIF.MakerNote": + case "EXIF.ComponentsConfiguration": + case "GPS.GPSVersion": + case "GPS.GPSAltitudeRef": + break; + default: +// echo "$key.$name=$val\n"; + } + } + } + + if (isset($lon) && isset($lat)) { + $json["geometry"] = [ + "type" => "Point", + "coordinates" => [$lon, $lat] + ]; + } + + $properties = [ + ]; + if (isset($filesize_bytes)) { + $properties["filesize_bytes"] = $filesize_bytes; + } + if (isset($height)) { + $properties["height_px"] = $height; + } + if (isset($width)) { + $properties["width_px"] = $width; + } + if (isset($orientation)) { + switch ($orientation) { + case 1: + $properties["orientation"] = 0; + case 3: + $properties["orientation"] = -180; + case 6: + $properties["orientation"] = 90; + case 8: + $properties["orientation"] = -90; + } + } + $json["properties"] = $properties; + + $s = json_encode($json); + file_put_contents($cacheFile, $s); + + return $json; + } + + if (!isset($_GET["file"])) { + $d = dir("."); + $features = []; + while (false !== ($entry = $d->read())) { + if(!isValidFilename($entry)) { + continue; + } + array_push($features, processFile($entry)); + } + $json = ["type" => "FeatureCollection", "features" => $features]; + } else { + $filename = $_GET["file"]; + $json = processFile($filename); + } + + header('Content-Type: application/vnd.geo+json'); + echo json_encode($json, JSON_PRETTY_PRINT); + echo "\n"; +} catch(InvalidArgumentException $e) { + header('HTTP/1.1 400 bad request'); +} +?> |