網頁

2013年1月31日 星期四

PHP Convert Exif GPS data to Google Map


Extract EXIF data using PHP to display GPS-tagged Images in Google Maps

PHP 取得EXIF中GPS的經緯度

【PHP】檔案上傳相關設定
 



class ExifReader{
 /**
* Returns GPS latitude & longitude as decimal values
**/ 
  public function getGPS($image)
  {
    $exif = exif_read_data($image, 0, true);
 echo $exif===false ? "No header data found.
\n" : "Image contains headers
\n";
    if (is_null($exif["GPS"])) echo "No GeoTag
";
  
    if ($exif){
      $lat = $exif['GPS']['GPSLatitude']; 
      $log = $exif['GPS']['GPSLongitude'];
      if (!$lat || !$log) return null;
  // latitude values ////緯度
      $lat_degrees = $this->gps2Num($lat[0]);
      $lat_minutes = $this->gps2Num($lat[1]);
      $lat_seconds = $this->gps2Num($lat[2]);
      $lat_hemi = $exif['GPS']['GPSLatitudeRef'];
 
  // longitude values ////經度
      $log_degrees = $this->gps2Num($log[0]);
      $log_minutes = $this->gps2Num($log[1]);
      $log_seconds = $this->gps2Num($log[2]);
      $log_hemi = $exif['GPS']['GPSLongitudeRef'];
 
      $lat_decimal = $this->toDecimal($lat_degrees, $lat_minutes, $lat_seconds, $lat_hemi);
      $log_decimal = $this->toDecimal($log_degrees, $log_minutes, $log_seconds, $log_hemi);
 
      return array($lat_decimal, $log_decimal);
      } else{
      return null;
    }
  }
 
  private function toDecimal($deg, $min, $sec, $hemi)
  {
    $d = $deg + $min/60 + $sec/3600;
    return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d;
  }
 
  private function gps2Num($coordPart)
  {
  // evaluate the string fraction and return a float // 
    $parts = explode('/', $coordPart);
  // prevent division by zero //
    if (!$parts[0] || !$parts[1]) { //  if(count($parts) <= 0)
      return 0;
    } else{
    return $parts[0] / $parts[1]; // floatval($parts[0]) / floatval($parts[1]);
    }

  }
}  
  
  //use in other php
  //include "ExifReader_class.php";
  // $classExifReader = new ExifReader;
  //$image = 'imagesName_with_gps_tag.jpg';
  //$latitudeAndLongitude = $classExifReader->getGPS($image);
  //print_r($latitudeAndLongitude); //看array裡是什麼東西的話 print_r






沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。