php - How to get geo location (coordinates) from image file? -
i've searched everywhere couldn't find solution, decided write own class in php. succeed in iphone image file , instagram image not sure if works other cameras android supported phones , cameras gps. can me out this? below code , implementation.
<?php class filer { /** * variables */ private $_gpslatitude; private $_gpslongitude; private $_gpslatituderef; private $_gpslongituderef; /** * constructor method */ public function __construct(){ /** * gps meta (in reference iphone's image file) * may android folow same thing (haven't checked) */ $this->_gpslatitude = 'gpslatitude'; $this->_gpslongitude = 'gpslongitude'; $this->_gpslongituderef = 'gpslatituderef'; $this->_gpslongituderef = 'gpslongituderef'; } /** * check if file contains gps information * @param: (string)$_file * @return: (array) file's exif data (if file contains gps information) */ public function getcoordinate($_file){ $_metas = $this->checkgps($_file); $_gps = $_metas['gps']; $_latitude = $this->dmstodec( $_gps[$this->_gpslatitude][0], $_gps[$this->_gpslatitude][1], $_gps[$this->_gpslatitude][2], $_gps[$this->_gpslongituderef] ); $_longitude = $this->dmstodec( $_gps[$this->_gpslongitude][0], $_gps[$this->_gpslongitude][1], $_gps[$this->_gpslongitude][2], $_gps[$this->_gpslongituderef] ); $_location = array($_latitude, $_longitude); return $_location; } /** * check if file contains gps information * @param: (string)$_file * @return: (array) file's exif data (if file contains gps information) */ public function checkgps($_file){ return exif_read_data($_file, 'gps', true); } /** * meta information of file * @param: (string)$_file * @return: (array) file's exif data * */ public function getexif($_file){ return exif_read_data($_file, 'ifd0', true); } /** * converts dms ( degrees / minutes / seconds ) * decimal format longitude / latitude * @param: (string)$_deg, (string)$_min, (string)$_sec, (string)$_ref * @return: (float) */ private function dmstodec($_deg, $_min, $_sec, $_ref){ $_array = explode('/', $_deg); $_deg = $_array[0]/$_array[1]; $_array = explode('/', $_min); $_min = $_array[0]/$_array[1]; $_array = explode('/', $_sec); $_sec = $_array[0]/$_array[1]; $_coordinate = $_deg+((($_min*60)+($_sec))/3600); /** * + + = north/east * + - = north/west * - - = south/west * - + = south/east */ if('s' === strtolower($_ref) || 'w' === strtolower($_ref)){ // negatify coordinate $_coordinate = 0-$_coordinate; } return $_coordinate; } /** * * converts decimal longitude / latitude dms * ( degrees / minutes / seconds ) * * piece of code may appear * inefficient, avoid issues floating * point math extract integer part , float * part using string function. * @param: (string)$_file * @return: (array) */ private function dectodms($_dec){ $_vars = explode(".", $_dec); $_deg = $vars[0]; $_tempma = "0.".$_vars[1]; $_tempma = $_tempma * 3600; $_min = floor($_tempma / 60); $_sec = $_tempma - ($_min*60); return array("deg"=>$_deg, "min"=>$_min, "sec"=>$_sec); } } // end class /** * * usage example * */ $_file = './image2.jpg'; $_filer = new filer(); if($_filer->checkgps($_file)){ $_location = $_filer->getcoordinate($_file); // file doesn't have gps information echo '<h4>file\'s gps information</h4>'; var_dump($_location); } else { // file doesn't have gps information echo '<h4>sorry file doesn\'t supply gps information</h4>'; var_dump($_filer->getexif($_file)); } ?>
the exiv2 documentation has excellent tags reference may identify fields may have missed. implementation otherwise looks pretty solid me, can't see glaring errors.
Comments
Post a Comment