| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* The exif2gmap simply adds Google map and Maker depends on geotag(EXIF gpsdata) in your <a href=> listed jpeg on node.
|
| 4 |
* Written by Takashi Ikebe (iktaka@gmail.com).
|
| 5 |
*/
|
| 6 |
|
| 7 |
|
| 8 |
$exif2gmap_debug=0;
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function exif2gmap_help($path, $arg) {
|
| 14 |
switch ($path) {
|
| 15 |
case 'admin/help#exif2gmap':
|
| 16 |
return '<p>'. t('Display exif2gmap functions.') .'</p>';
|
| 17 |
break;
|
| 18 |
case "admin/modules#description":
|
| 19 |
return '<p>'. t('Control exif2gmap functions.') .'</p>';
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_perm().
|
| 25 |
*/
|
| 26 |
function exif2gmap_perm() {
|
| 27 |
return array('access exif2gmap', 'administer exif2gmap');
|
| 28 |
}
|
| 29 |
|
| 30 |
function exif2gmap_admin_settings() {
|
| 31 |
|
| 32 |
$form['exif2gmap'] = array(
|
| 33 |
'#type' => 'fieldset',
|
| 34 |
'#title' => t('exif2gmap settings'),
|
| 35 |
'#description' => t('Create the google map and marker use geotag infomation in exif.'),
|
| 36 |
);
|
| 37 |
|
| 38 |
$form['exif2gmap']['exif2gmap_valid'] = array(
|
| 39 |
'#type' => 'checkbox',
|
| 40 |
'#title' => t('Enable exif2gmap filter'),
|
| 41 |
'#return_value' => 1,
|
| 42 |
'#default_value' => variable_get('exif2gmap_valid', 0),
|
| 43 |
|
| 44 |
);
|
| 45 |
|
| 46 |
$form['exif2gmap']['exif2gmap_MapKey'] = array(
|
| 47 |
'#type' => 'textfield',
|
| 48 |
'#title' => t('Google Map API Key'),
|
| 49 |
'#default_value'=> variable_get('exif2gmap_MapKey', ''),
|
| 50 |
'#size' => 50,
|
| 51 |
'#maxlength' => 100,
|
| 52 |
'#description' => t('Google Map API Key(http://code.google.com/apis/maps/)'),
|
| 53 |
'#attributes' => NULL,
|
| 54 |
'#required' => TRUE,
|
| 55 |
);
|
| 56 |
|
| 57 |
$form['exif2gmap']['exif2gmap_GMwidth'] = array(
|
| 58 |
'#type' => 'textfield',
|
| 59 |
'#title' => t('google map size(width)'),
|
| 60 |
'#default_value'=> variable_get('exif2gmap_GMwidth', ''),
|
| 61 |
'#size' => 10,
|
| 62 |
'#maxlength' => 15,
|
| 63 |
'#description' => t('Enter width of map size.(Like 600px)'),
|
| 64 |
'#attributes' => NULL,
|
| 65 |
'#required' => TRUE,
|
| 66 |
);
|
| 67 |
$form['exif2gmap']['exif2gmap_GMheight'] = array(
|
| 68 |
'#type' => 'textfield',
|
| 69 |
'#title' => t('google map size(height)'),
|
| 70 |
'#default_value'=> variable_get('exif2gmap_GMheight', ''),
|
| 71 |
'#size' => 10,
|
| 72 |
'#maxlength' => 15,
|
| 73 |
'#description' => t('Enter width of map size.(Like 400px)'),
|
| 74 |
'#attributes' => NULL,
|
| 75 |
'#required' => TRUE,
|
| 76 |
);
|
| 77 |
|
| 78 |
return system_settings_form($form);
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Implementation of hook_menu().
|
| 83 |
*/
|
| 84 |
function exif2gmap_menu() {
|
| 85 |
$items = array();
|
| 86 |
|
| 87 |
$items['admin/settings/exif2gmap'] = array(
|
| 88 |
'title' => 'exif2gmap',
|
| 89 |
'description' => 'Control which and where exif2gmap links should be active.',
|
| 90 |
'page callback' => 'drupal_get_form',
|
| 91 |
'page arguments' => array('exif2gmap_admin_settings'),
|
| 92 |
'access arguments' => array('administer exif2gmap'),
|
| 93 |
'type' => MENU_NORMAL_ITEM,
|
| 94 |
);
|
| 95 |
|
| 96 |
return $items;
|
| 97 |
}
|
| 98 |
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_filter(), which enables exif2gmap keyword automatic link.
|
| 102 |
*/
|
| 103 |
|
| 104 |
function exif2gmap_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 105 |
switch ($op) {
|
| 106 |
case 'list':
|
| 107 |
return array(
|
| 108 |
0 => t('exif2gmap'),
|
| 109 |
);
|
| 110 |
|
| 111 |
case 'no cache':
|
| 112 |
switch ($delta) {
|
| 113 |
case 0:
|
| 114 |
return 0;
|
| 115 |
default:
|
| 116 |
return 0;
|
| 117 |
}
|
| 118 |
|
| 119 |
case 'description':
|
| 120 |
switch ($delta) {
|
| 121 |
case 0:
|
| 122 |
return t('Enable exif2gmap.');
|
| 123 |
default:
|
| 124 |
return;
|
| 125 |
}
|
| 126 |
|
| 127 |
case 'process':
|
| 128 |
switch ($delta) {
|
| 129 |
case 0:
|
| 130 |
if(($text != NULL)&&variable_get('exif2gmap_valid','')){
|
| 131 |
$temp= exif2gmap_process($text, $format);
|
| 132 |
if($temp == NULL){
|
| 133 |
return $text;
|
| 134 |
}
|
| 135 |
return $temp;
|
| 136 |
}
|
| 137 |
default:
|
| 138 |
return $text;
|
| 139 |
}
|
| 140 |
|
| 141 |
case 'settings':
|
| 142 |
switch ($delta) {
|
| 143 |
case 0:
|
| 144 |
return exif2gmap_admin_settings($format);
|
| 145 |
default:
|
| 146 |
return;
|
| 147 |
}
|
| 148 |
|
| 149 |
default:
|
| 150 |
return $text;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
function get_gpsdata($filename){
|
| 157 |
if($exif2gmap_debug){
|
| 158 |
drupal_set_message("get_gpsdata called ". $filename);
|
| 159 |
}
|
| 160 |
|
| 161 |
$exif = exif_read_data($filename,'COMPUTED');
|
| 162 |
if($exif==FALSE){
|
| 163 |
if($exif2gmap_debug){
|
| 164 |
drupal_set_message("get_gpsdata failed\n");
|
| 165 |
}
|
| 166 |
}
|
| 167 |
|
| 168 |
if($exif2gmap_debug){
|
| 169 |
drupal_set_message("exif=". print_r($exif));
|
| 170 |
}
|
| 171 |
$GPS=array();
|
| 172 |
foreach ($exif as $key=>$value)
|
| 173 |
{
|
| 174 |
if($key == 'GPSLatitudeRef'){
|
| 175 |
$GPS['GPSLatitudeRef']=$value;
|
| 176 |
}
|
| 177 |
if($key == 'GPSLongitudeRef' ){
|
| 178 |
$GPS['GPSLongitudeRef']=$value;
|
| 179 |
}
|
| 180 |
if($key == 'GPSLatitude'){
|
| 181 |
$GPS['GPSLatitude']=$value;
|
| 182 |
}
|
| 183 |
if($key == 'GPSLongitude'){
|
| 184 |
$GPS['GPSLongitude']=$value;
|
| 185 |
}
|
| 186 |
}
|
| 187 |
if(isset($GPS['GPSLatitude'])){
|
| 188 |
return $GPS;
|
| 189 |
}
|
| 190 |
return NULL;
|
| 191 |
}
|
| 192 |
|
| 193 |
|
| 194 |
function get_xxxitude( $gps, $type = 'lat' ) {
|
| 195 |
$prefix = array( 'N' => '', 'S' => '-', 'E' => '', 'W' => '-' );
|
| 196 |
$num = $type != 'lon' ? 'GPSLatitude' : 'GPSLongitude';
|
| 197 |
$ref = $num . 'Ref';
|
| 198 |
if ( ! isset( $gps[$ref] ) || ! isset( $prefix[$gps[$ref]] ) ) return '';
|
| 199 |
if ( ! isset( $gps[$num] ) || ! is_array( $gps[$num] ) ) return '';
|
| 200 |
$temp = array();
|
| 201 |
foreach ( $gps[$num] as $expr ) eval("\$temp[] = $expr;");
|
| 202 |
if($exif2gmap_debug){
|
| 203 |
drupal_set_message($ref .":".$gps[$ref]. ":".$prefix[$gps[$ref]]);
|
| 204 |
}
|
| 205 |
$ret=$prefix[$gps[$ref]];
|
| 206 |
$ret .= $temp[0]+($temp[1]/60)+($temp[2]/(60*60));
|
| 207 |
return $ret;
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Core part of exif2gmap google map marker.
|
| 212 |
*
|
| 213 |
*/
|
| 214 |
function exif2gmap_process($text, $format = -1) {
|
| 215 |
//parse text in order to check gps exif.
|
| 216 |
$mapkey=variable_get('exif2gmap_MapKey','');
|
| 217 |
$width=variable_get('exif2gmap_GMwidth', '');
|
| 218 |
$height=variable_get('exif2gmap_GMheight', '');
|
| 219 |
|
| 220 |
$url = trim(url('<front>', array('absolute' => TRUE)),"/");
|
| 221 |
|
| 222 |
|
| 223 |
$res=array();
|
| 224 |
|
| 225 |
preg_match_all('/<a.*href=["\']([^"\']*)["\'][^>]*>(.*?)<\/a>/', $text, $res,PREG_SET_ORDER);
|
| 226 |
$cnt=count($res);
|
| 227 |
if($exif2gmap_debug){
|
| 228 |
drupal_set_message($text);
|
| 229 |
drupal_set_message(print_r($res));
|
| 230 |
drupal_set_message("count=".$cnt);
|
| 231 |
}
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
$ans=array();
|
| 237 |
$j=-1;
|
| 238 |
for($i=0;$i<$cnt;$i++){
|
| 239 |
$pos=strrpos($res[$i][1],'.');
|
| 240 |
if($pos!=FALSE){
|
| 241 |
$type=NULL;
|
| 242 |
$type=strtolower(substr($res[$i][1],$pos+1,strlen($res[$i][1])));
|
| 243 |
if($exif2gmap_debug){
|
| 244 |
drupal_set_message("res=". $res[$i][1]. " type=" .$type);
|
| 245 |
}
|
| 246 |
if(strcmp($type,'jpg')!=0 && strcmp($type,'jpeg')!=0){
|
| 247 |
//type is not jpeg..
|
| 248 |
if($exif2gmap_debug){
|
| 249 |
drupal_set_message("type is not jpeg". $type);
|
| 250 |
}
|
| 251 |
|
| 252 |
$gps=NULL;
|
| 253 |
}else{
|
| 254 |
//check basepath.
|
| 255 |
$head=strtolower(substr($res[$i][1],0,4));
|
| 256 |
if($exif2gmap_debug){
|
| 257 |
drupal_set_message($head);
|
| 258 |
}
|
| 259 |
if(strcmp($head,"http")!=0){
|
| 260 |
$fname=$url.$res[$i][1];
|
| 261 |
}else{
|
| 262 |
$fname=$res[$i][1];
|
| 263 |
}
|
| 264 |
|
| 265 |
$gps=get_gpsdata($fname);
|
| 266 |
if($gps!=NULL){
|
| 267 |
$j++;
|
| 268 |
$ans[$j]=array();
|
| 269 |
$ans[$j]['lat']=get_xxxitude( $gps, 'lat' );
|
| 270 |
$ans[$j]['lon']=get_xxxitude( $gps, 'lon' );
|
| 271 |
$ans[$j]['fpath']=$fname;
|
| 272 |
$ans[$j]['tag']=$res[$i][0];
|
| 273 |
|
| 274 |
}
|
| 275 |
}
|
| 276 |
}
|
| 277 |
}
|
| 278 |
if($exif2gmap_debug){
|
| 279 |
drupal_set_message(print_r($ans));
|
| 280 |
drupal_set_message("latnum=" .count($ans));
|
| 281 |
drupal_set_message("gps=". print_r($gps));
|
| 282 |
}
|
| 283 |
//Map Center point.
|
| 284 |
$Clat=0;
|
| 285 |
$Clon=0;
|
| 286 |
if(count($ans)==0){
|
| 287 |
return $text;
|
| 288 |
}
|
| 289 |
$SW=260;
|
| 290 |
$NE=-260;
|
| 291 |
for($i=0;$i<count($ans);$i++){
|
| 292 |
$Clat += $ans[$i]['lat'];
|
| 293 |
$Clon += $ans[$i]['lon'];
|
| 294 |
if(($ans[$i]['lat']+$ans[$i]['lon'])>$NE){
|
| 295 |
$NE=$ans[$i]['lat']+$ans[$i]['lon'];
|
| 296 |
$NElat=$ans[$i]['lat'];
|
| 297 |
$NElon=$ans[$i]['lon'];
|
| 298 |
}
|
| 299 |
if(($ans[$i]['lat']+$ans[$i]['lon'])<$SW){
|
| 300 |
$SW=$ans[$i]['lat']+$ans[$i]['lon'];
|
| 301 |
$SWlat=$ans[$i]['lat'];
|
| 302 |
$SWlon=$ans[$i]['lon'];
|
| 303 |
}
|
| 304 |
}
|
| 305 |
$Clat=$Clat/count($ans);
|
| 306 |
$Clon=$Clon/count($ans);
|
| 307 |
|
| 308 |
if($exif2gmap_debug){
|
| 309 |
drupal_set_message("Clat=" .$Clat." Clon=".$Clon);
|
| 310 |
}
|
| 311 |
|
| 312 |
|
| 313 |
//google map javascript maker...
|
| 314 |
$digest=hash("sha256",$text);
|
| 315 |
|
| 316 |
$script = 'var map; window.onload= function load(){ if (GBrowserIsCompatible()) {
|
| 317 |
map = new GMap2(document.getElementById("map'.$digest.'"));
|
| 318 |
';
|
| 319 |
|
| 320 |
$script .= 'map.setCenter(new GLatLng(' .$Clat. ',' .$Clon. '), 7);
|
| 321 |
';
|
| 322 |
$script .= 'map.addControl(new GLargeMapControl());
|
| 323 |
';
|
| 324 |
|
| 325 |
$script .= 'var rectObj = new GLatLngBounds(new GLatLng('.$SWlat.','.$SWlon.'), new GLatLng('.$NElat.','.$NElon.'));';
|
| 326 |
$script .= 'var zm = map.getBoundsZoomLevel(rectObj); map.setZoom(zm-1)';
|
| 327 |
|
| 328 |
//Marker part.
|
| 329 |
for($i=0;$i<count($ans);$i++){
|
| 330 |
|
| 331 |
$script .= '
|
| 332 |
var marker'.$i.'= new GMarker(new GLatLng(' .$ans[$i]['lat']. ',' .$ans[$i]['lon']. '));';
|
| 333 |
$script .= 'GEvent.addListener(marker'.$i.', "mouseover", function(){';
|
| 334 |
$tag=str_replace( '"','\'',$ans[$i]['tag']);
|
| 335 |
$script .= 'marker'.$i.'.openInfoWindowHtml("' .$tag. '");});';
|
| 336 |
$script .= 'map.addOverlay(marker'.$i.');';
|
| 337 |
}
|
| 338 |
|
| 339 |
$script .='}}';
|
| 340 |
|
| 341 |
//inline coding..
|
| 342 |
drupal_set_html_head('<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&key='.$mapkey.'"></script>');
|
| 343 |
drupal_add_js($script,'inline');
|
| 344 |
|
| 345 |
//add div portion.
|
| 346 |
$text .= '<br><div id="map'.$digest.'" style="width: '.$width.'; height: '.$height.'"></div>';
|
| 347 |
return $text;
|
| 348 |
}
|
| 349 |
|