<?php
	echo "var json_fotos ={";
	echo "'type':'FeatureCollection',";
	echo "	   'name':'fotos',						";
	echo "	   'crs':{                                          ";
	echo "		  'type':'name',                                ";
	echo "		  'properties':{                                ";
	echo "			 'name':'urn:ogc:def:crs:OGC:1.3:CRS84'     ";
	echo "		  }                                             ";
	echo "	   },                                               ";
	echo "	   'features':[                                     ";



	$path = "../".$_GET["path"];
	
	
	$dh = opendir($path);
	$i=1;
	while (($file = readdir($dh)) !== false) {
		if(str_ends_with($file,'jpg') or str_ends_with($file,'jpeg') or str_ends_with($file,'JPG') or str_ends_with($file,'JPEG')) {


		//image file path
		$imageURL = $path."/".$file;

		//get location of image
		$imgData = get_image_data($imageURL);

		// latitude & longitude
			 $imgLat = $imgData['latitude'];
			 $imgLng = $imgData['longitude'];
			 $DateTime = $imgData['DateTimeOriginal'];

//		$imgLat = $imgLocation['latitude'] != 0 ? $imgLocation['latitude'] : rand(0, 10) / 10;
//		$imgLng = $imgLocation['longitude'] != 0 ? $imgLocation['longitude'] : rand(0, 10) / 10;
//		$DateTime = $imgData['DateTimeOriginal'];

				echo "		  {                                             ";
				echo "			 'type':'Feature',                          ";
				echo "			 'properties':{                             ";
				echo "				'id':'$i',                              ";
				echo "				'Name':'$file',           		        ";
				echo "				'Date':'$DateTime'           		    ";
				echo "			 },                                         ";
				echo "			 'geometry':{                               ";
				echo "				'type':'Point',                         ";
				echo "				'coordinates':[                         ";
				echo "				   $imgLng,                    			";
				echo "				   $imgLat                    			";
				echo "				]                                       ";
				echo "			 }                                          ";
				echo "		  },                                            ";


			$i++;
		}
	}
	closedir($dh);

	echo "	   ]                                                ";
	echo "	}                                                   ";
	






function get_image_data($image = ''){
    $exif = exif_read_data($image, 0, true);
    if($exif && isset($exif['GPS']) && isset($exif['GPS']['GPSLatitudeRef']) ){
        $GPSLatitudeRef     = $exif['GPS']['GPSLatitudeRef'];
        $GPSLatitude        = $exif['GPS']['GPSLatitude'];
        $GPSLongitudeRef    = $exif['GPS']['GPSLongitudeRef'];
        $GPSLongitude       = $exif['GPS']['GPSLongitude'];
        $DateTimeOriginal   = $exif['IFD0']['DateTime'];
        
        $lat_degrees = count($GPSLatitude) > 0 ? gps2Num($GPSLatitude[0]) : 0;
        $lat_minutes = count($GPSLatitude) > 1 ? gps2Num($GPSLatitude[1]) : 0;
        $lat_seconds = count($GPSLatitude) > 2 ? gps2Num($GPSLatitude[2]) : 0;
        
        $lon_degrees = count($GPSLongitude) > 0 ? gps2Num($GPSLongitude[0]) : 0;
        $lon_minutes = count($GPSLongitude) > 1 ? gps2Num($GPSLongitude[1]) : 0;
        $lon_seconds = count($GPSLongitude) > 2 ? gps2Num($GPSLongitude[2]) : 0;
        
        $lat_direction = ($GPSLatitudeRef == 'W' or $GPSLatitudeRef == 'S') ? -1 : 1;
        $lon_direction = ($GPSLongitudeRef == 'W' or $GPSLongitudeRef == 'S') ? -1 : 1;
        
        $latitude = $lat_direction * ($lat_degrees + ($lat_minutes / 60) + ($lat_seconds / (60*60)));
        $longitude = $lon_direction * ($lon_degrees + ($lon_minutes / 60) + ($lon_seconds / (60*60)));

        return array('latitude'=>$latitude, 'longitude'=>$longitude, 'DateTimeOriginal'=>$DateTimeOriginal);
    }else{
        return array('latitude'=>rand(0, 10) / 10, 'longitude'=>rand(0, 10) / 10, 'DateTimeOriginal'=>$exif['IFD0']['DateTime']);
		// return false;
    }
}	
	
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]);
}
	
	
	
?> 






 