phpgpsgeotagging

How to Upload Photo and return GPS co-ords


All, I'm tinkering with a simp[le form to upload a photo, store it within my server & then return the GPS co-ordinates. I'm using the standard PHP file upload script and a GPs solution i found here. It allows me to upload files but does not return the GPS co-ordinates. Can anyone help identify the issue please? My complete php is:

				<?php
				$target_dir = "uploads/";
				$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
				$uploadOk = 1;
				$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
				// Check if image file is a actual image or fake image
				if(isset($_POST["submit"])) {
					$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
					if($check !== false) {
						echo "File is an image - " . $check["mime"] . ".";
						$uploadOk = 1;
					} else {
						echo "File is not an image.";
						$uploadOk = 0;
					}
				}
				// Check if file already exists
				if (file_exists($target_file)) {
					echo "Sorry, file already exists.";
					$uploadOk = 0;
				}
				// Check file size
				if ($_FILES["fileToUpload"]["size"] > 5000000000) {
					echo "Sorry, your file is too large.";
					$uploadOk = 0;
				}
				// Allow certain file formats
				if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
				&& $imageFileType != "gif" ) {
					echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
					$uploadOk = 0;
				}
				// Check if $uploadOk is set to 0 by an error
				if ($uploadOk == 0) {
					echo "Sorry, your file was not uploaded.";
				// if everything is ok, try to upload file
				} else {
					if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
						echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
					} else {
						echo "Sorry, there was an error uploading your file.";
					}
				}

				read_gps_location($target_file);

				/**
				 * Returns an array of latitude and longitude from the Image file
				 * @param image $file
				 * @return multitype:number |boolean
				 */
				function read_gps_location(){
					if (is_file($target_file)) {
						$info = exif_read_data($target_file);
						if (isset($info['GPSLatitude']) && isset($info['GPSLongitude']) &&
							isset($info['GPSLatitudeRef']) && isset($info['GPSLongitudeRef']) &&
							in_array($info['GPSLatitudeRef'], array('E','W','N','S')) && in_array($info['GPSLongitudeRef'], array('E','W','N','S'))) {

							$GPSLatitudeRef  = strtolower(trim($info['GPSLatitudeRef']));
							$GPSLongitudeRef = strtolower(trim($info['GPSLongitudeRef']));

							$lat_degrees_a = explode('/',$info['GPSLatitude'][0]);
							$lat_minutes_a = explode('/',$info['GPSLatitude'][1]);
							$lat_seconds_a = explode('/',$info['GPSLatitude'][2]);
							$lng_degrees_a = explode('/',$info['GPSLongitude'][0]);
							$lng_minutes_a = explode('/',$info['GPSLongitude'][1]);
							$lng_seconds_a = explode('/',$info['GPSLongitude'][2]);

							$lat_degrees = $lat_degrees_a[0] / $lat_degrees_a[1];
							$lat_minutes = $lat_minutes_a[0] / $lat_minutes_a[1];
							$lat_seconds = $lat_seconds_a[0] / $lat_seconds_a[1];
							$lng_degrees = $lng_degrees_a[0] / $lng_degrees_a[1];
							$lng_minutes = $lng_minutes_a[0] / $lng_minutes_a[1];
							$lng_seconds = $lng_seconds_a[0] / $lng_seconds_a[1];

							$lat = (float) $lat_degrees+((($lat_minutes*60)+($lat_seconds))/3600);
							$lng = (float) $lng_degrees+((($lng_minutes*60)+($lng_seconds))/3600);

							//If the latitude is South, make it negative. 
							//If the longitude is west, make it negative
							$GPSLatitudeRef  == 's' ? $lat *= -1 : '';
							$GPSLongitudeRef == 'w' ? $lng *= -1 : '';

							return array(
								'lat' => $lat,
								'lng' => $lng
							);
						}           
					}
					return false;
				} 


				?> 


Solution

  • You invoke function read_gps_location($target_file); but you don't print the output

    try changing this line

    read_gps_location($target_file);
    

    to

    print_r(read_gps_location($target_file)); 
    

    To see the output. It should be either array or false.