<?php
// read_exif_off_image_rotate.php
// RJM Programming
// August, 2016
// Thanks to http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data


$image="";
$ximage="";

if (isset($_GET['image'])) {
  $image = urldecode($_GET['image']);
} else if (isset($_POST['image'])) {
  $image = urldecode($_POST['image']);
} else if (file_exists("/Applications/MAMP/htdocs/clouds.jpg")) {
  $image = "/Applications/MAMP/htdocs/clouds.jpg";
}




$javascript="<scr" . "ipt type='text/javascript'> 
</sc" . "ript> ";

if (isset($_GET['donothing'])) {
 echo "<html></html>";
} else {
$nform="<br><form target='_self' id='myform' action='./read_exif_off_image_rotate.php' method='POST'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image URL: <input name='image' id='image' onblur=' ob(this.value); ' type='text' value='' style='width:40%;'></input>&nbsp;&nbsp;<input onclick=' setTimeout(doalert,1000); ' type='submit' value='Exif Information'></input></form>";
$rotbit="";

$htmlis="";
$indiv="";

$maptemplate="http://www.rjmprogramming.com.au/PHP/Map/map.php?title=&onclick=y&label=[%27Lat%27,&value=%27Lon%27,%20%27Name%27]&data=,%20[-33.967,151.2422,~~]";
$factor=1.0;
$sbit="";
$various=" ";
$wasvarious="youl" . "lneverfin" . "dthis";

if (isset($_GET['image'])) {
  $image = urldecode($_GET['image']);
} else if (isset($_POST['image'])) {
  $image = urldecode($_POST['image']);
} else if (file_exists("/Applications/MAMP/htdocs/clouds.jpg")) {
  $image = "/Applications/MAMP/htdocs/clouds.jpg";
} 

$types = array(
1 => "GIF",
2 => "JPEG",
3 => "PNG",
4 => "SWF",
5 => "PSD",
6 => "BMP",
7 => "TIFF",
8 => "TIFF"
);


$imagetype = exif_imagetype($image);

if ($types[$imagetype] == "PNG") {
     

        // Imagick Type  // thanks to http://php.net/manual/en/imagick.getimageproperties.php
        // ---------------------------------------------

    $imagick_type = new Imagick($image);
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = $image;
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Properties
        // ---------------------------------------------
       
    $imagick_type_properties = $imagick_type->getImageProperties('*', FALSE);
   
        // Print Image Properties
        // ---------------------------------------------
   
    print("<pre>");
       
    print_r($imagick_type_properties);
   
        // Print Each Individual, Image Property
        // ---------------------------------------------
   
    foreach($imagick_type_properties as $value)
    {
        print("$value --- ");
        print($imagick_type->getImageProperty("$value"));
        print("<br><br>");
    }
               
    print("</pre>");
    
} else {

if (array_key_exists($imagetype, $types)) {
 $htmlis="<html><head>" . $javascript . "<style> div { font-size:9px; } </style><meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes'><title>Exif for " . $image . "</title></head><body style='background-color:pink;'><h1 id='myh1'>Exif for " . $image . "</h1><h3>RJM Programming - August, 2016  <a id='ageolocation style='visibility:hidden;' href='#mygeolocation'>Geolocation Context for Photograph" . $various . "</a></h3><h4>Thanks to <a target=_blank title=Useful href=http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data>http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data</a></h4><div style='background-color:yellow; background: url(" . str_replace("/Applications/MAMP/htdocs/","http://localhost:8888/",$image) . "); norepeat; '><br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image type of " . $image . " is: " . $types[$imagetype];
 $exif = exif_read_data($image, 0, true);
 foreach ($exif as $key => $section) {
  foreach ($section as $name => $val) {
   if (sizeof($val) > 1) {
   $indiv.="<br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$key.$name: " . $val[0];
   $nthing=0.0;
   $vs=explode("/",$val[0]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
   if ($key == "GPS" && $name == "GPSAltitude") {
   $vs=explode("/",$val[0]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=" up at " . ($vs[0] / $vs1) / ($factor);
     $htmlis=str_replace($wasvarious, $wasvarious . " up at " . ($vs[0] / $vs1) / ($factor), $htmlis);
   }

   if ($key == "EXIF" && $name == "ShutterSpeedValue") {
   $vs=explode("/",$val[0]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=" with (" . ($vs[0] / $vs1) . ",";
   }

   if ($key == "EXIF" && $name == "ApertureValue") {
   $vs=explode("/",$val[0]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=($vs[0] / $vs1) . ")";
   }


   if ($key == "GPS" && $name == "GPSLatitude") {
     $nthing+=($vs[0] / $vs1) / ($factor);
     $factor*=60; 
   } else if ($key == "GPS" && $name == "GPSLongitude") {
     $nthing+=($vs[0] / $vs1) / ($factor);
     $factor*=60; 
   }
   for ($jj=1; $jj<sizeof($val); $jj++) {
   if ($key == "GPS" && $name == "GPSLatitude") {
   $vs=explode("/",$val[$jj]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $nthing+=($vs[0] / $vs1) / ($factor);
     $factor*=60; 
   } else if ($key == "GPS" && $name == "GPSLongitude") {
   $vs=explode("/",$val[$jj]);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $nthing+=($vs[0] / $vs1) / ($factor);
     $factor*=60; 
   }
    $indiv.=',' . $val[$jj];
   }
   if ($key == "GPS" && $name == "GPSLatitude") {
     $maptemplate=str_replace("-33.967", $nthing, $maptemplate);
     $various.=" at (" . $nthing . ",";
   } else if ($key == "GPS" && $name == "GPSLongitude") {
     $maptemplate=str_replace("151.2422", $nthing, $maptemplate);
     $various.=$nthing . ")";
     //$maptemplate=str_replace("&onclick=y", urlencode($various) . "&onclick=y", $maptemplate);
     //$sbit=" <scri" . "pt type='text/javascript'> window.open(\"" . $maptemplate . "\",\"_blank\",\"top=50,left=50,height=500,width=500\"); </sc" . "ript> ";
     $sbit=" <iframe onload=\" location.href='#myh1'; \" id=\"mygeolocation\" title=\"Geolocation context\" src=\"" . $maptemplate . "\"></iframe>";
     $wasvarious="Geolocation Context for Photograph" . $various;
     $htmlis=str_replace("Geolocation Context for Photograph ", "Geolocation Context for Photograph" . $various, str_replace("visibility:hidden", "visibility:visible", $htmlis));
   }
   } else {
   if ($key == "FILE" && $name == "FileName") {
     $maptemplate=str_replace("~~", "~" . urlencode($val) . "~", $maptemplate);
     $maptemplate=str_replace("title=&", "title=" . urlencode($val) . urlencode($various) . "&", $maptemplate);

   } else if ($key == "EXIF" && $name == "ShutterSpeedValue") {
   $vs=explode("/",$val);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=" with (" . ($vs[0] / $vs1) . ",";
   

   } else if ($key == "EXIF" && $name == "ApertureValue") {
   $vs=explode("/",$val);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=($vs[0] / $vs1) . ")";
   
   } else if ($key == "EXIF" && $name == "DateTimeOriginal") {
     $various.=" on " . $val;
   } else if ($key == "GPS" && $name == "GPSAltitude") {
   $vs=explode("/",$val);
   if (sizeof($vs) == 1) {
     $vs1=1.0;
   } else {
     $vs1=$vs[1];
   }
     $various.=" up at " . ($vs[0] / $vs1) / ($factor);
     $htmlis=str_replace($wasvarious, $wasvarious . " up at " . ($vs[0] / $vs1) / ($factor), $htmlis);
   } else if ($key == "GPS" && $name == "GPSAltitudeRef" && trim($val) != "") {
     $factor=-1.0; 
   } else if ($key == "GPS" && $name == "GPSAltitudeRef" && trim($val) == "") {
     $factor=1.0; 
   } else if ($key == "GPS" && $name == "GPSLatitudeRef" && $val == "S") {
     $factor=-1.0; 
   } else if ($key == "GPS" && $name == "GPSLongitudeRef" && $val == "W") {
     $factor=-1.0; 
   } else if ($key == "GPS" && $name == "GPSLatitudeRef" && $val == "N") {
     $factor=1.0; 
   } else if ($key == "GPS" && $name == "GPSLongitudeRef" && $val == "E") {
     $factor=1.0; 
   }
   $indiv.="<br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$key.$name: $val";
   }
   if (($key.$name) == "IFD0Orientation") {
     if (str_replace(" ","","$val") == "8") {
       //$htmlis="<!doctype html><html><head>" . $javascript . "<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.1, maximum-scale=8, user-scalable=yes'><title>Exif for " . $image . "</title></head><body style='background-color:yellow; background: url(" . str_replace("/Applications/MAMP/htdocs/","http://localhost:8888/",$image) . "); norepeat; " . $rotbit . "'><h1 id='myh1'>Exif for " . $image . "</h1><h3>RJM Programming - August, 2016</h3><h4>Thanks to <a target=_blank title=Useful href=http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data>http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data</a></h4><div><br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image type of " . $image . " is: " . $types[$imagetype];
       $htmlis=str_replace("; norepeat; ", "; opacity:0.6; norepeat; -webkit-transform: rotate(270deg); -moz-transform: rotate(270deg); -ms-transform: rotate(270deg); -o-transform: rotate(270deg); transform: rotate(270deg); ", $htmlis);
     } else if (str_replace(" ","","$val") == "6") {
       //$htmlis="<!doctype html><html><head>" . $javascript . "<title>Exif for " . $image . "</title></head><body style='background-color:yellow; background: url(" . str_replace("/Applications/MAMP/htdocs/","http://localhost:8888/",$image) . "); norepeat; " . $rotbit . "'><h1>Exif for " . $image . "</h1><h3>RJM Programming - August, 2016</h3><h4>Thanks to <a target=_blank title=Useful href=http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data>http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data</a></h4><div><br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image type of " . $image . " is: " . $types[$imagetype];
       $htmlis=str_replace("; norepeat; ", "; opacity:0.6;  norepeat; -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -ms-transform: rotate(90deg); -o-transform: rotate(90deg); transform: rotate(90deg); ", $htmlis);
     } else if (str_replace(" ","","$val") == "3") {
       //$htmlis="<!doctype html><html><head>" . $javascript . "<title>Exif for " . $image . "</title></head><body style='background-color:yellow; background: url(" . str_replace("/Applications/MAMP/htdocs/","http://localhost:8888/",$image) . "); norepeat; " . $rotbit . "'><h1>Exif for " . $image . "</h1><h3>RJM Programming - August, 2016</h3><h4>Thanks to <a target=_blank title=Useful href=http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data>http://www.v-nessa.net/2010/08/02/using-php-to-extract-image-exif-data</a></h4><div><br>" . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image type of " . $image . " is: " . $types[$imagetype];
       $htmlis=str_replace("; norepeat; ", "; opacity:0.6;  norepeat; -webkit-transform: rotate(180deg); -moz-transform: rotate(180deg); -ms-transform: rotate(180deg); -o-transform: rotate(180deg); transform: rotate(180deg); ", $htmlis);
     }
   }
  }
 }
 if (str_replace("transXXXform:","",$htmlis) != $htmlis) {
   echo str_replace("</h4><div", "</h4>" . $indiv . "<div", $htmlis) . "</div>" . $nform . $sbit . "</body></html>";
 } else {
   echo $htmlis . $indiv . "</div>" . $nform . $sbit . "</body></html>";
 }
} else {
 echo "<br>" . "Not a valid image type for " . $image . " or Exif functionality not enabled." . $nform;
}
}
}
?>