Submit Your Article Forum Rules

Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: Php script problem.

  1. #21
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793

    Working draft of gps_exif.php

    gps_exif_inc.php
    PHP Code:
    <?php 
    function readExif($img) {
     if (
    file_exists($img)) {
      global 
    $exif$gpsAltitude$gpsDecimalLat$gpsDecimalLon;
      
    $exif read_exif_data($img0true);
      
    $fileName $exif['FILE']['FileName'];
      
    $fileSize $exif['FILE']['FileSize'];
      if (
    $fileSize >= 1024) { $imageFileSize number_format($fileSize 10242).'kb';}
      elseif (
    $fileSize >= 1048576) {$imageFileSize number_format($fileSize 10485762).'mb';}
      
    $gpsAltitude $exif["GPS"]["GPSAltitude"][0];    $gps getGPS();
      if (
    $gps != null) {
       
    $gpsDecimalLat $gps[0];
       
    $gpsDecimalLon $gps[1];
      }
     }
    }
    function 
    print_data() {
     global 
    $exif$gpsAltitude$lat_degrees$lat_minutes$lat_seconds$lat_hemi$gpsDecimalLat$log_degrees$log_minutes$log_seconds$log_hemi$gpsDecimalLon;
     
    $str "    <tr>\n";
     
    $str .= "     <td>\n      <p>Altitude: $gpsAltitude</p>\n     </td>\n";
     
    $str .= "     <td>\n      <p>Degrees: $lat_degrees</p>\n      <p>Minutes: $lat_minutes</p>\n      <p>Seconds: $lat_seconds</p>\n      <p>Latitude Hemisphere: $lat_hemi</p>\n      <p>Decimal Latitude: $gpsDecimalLat</p>\n     </td>\n";
     
    $str .= "     <td>\n      <p>Degrees: $log_degrees</p>\n      <p>Minutes: $log_minutes</p>\n      <p>Seconds: $log_seconds</p>\n      <p>Longitude Hemisphere: $log_hemi</p>\n      <p>Decimal Longitude: $gpsDecimalLon</p>\n     </td>\n";
     
    $str .= "    </tr>\n";
     echo 
    $str;
    }
    function 
    getImg($img) {
     global 
    $exif;
     
    $fileWidth $exif['COMPUTED']['Width'];
     
    $fileHeight $exif['COMPUTED']['Height'];
     if (
    file_exists($img)) {
      
    $str "   <img src=\"$img\" width=\"$fileWidth\" height=\"$fileHeight\" alt=\"" strTrunc($img,4) . "\">\n";
      echo 
    $str;
     } else {
      echo 
    "Image not found.";
     }
    }
    // truncate $str by $trunc characters - my own utility
    function strTrunc($str,$trunc) { return substr($str,0,strlen($str)-$trunc);};

    // adapted from http://www.quietless.com/kitchen/extract-exif-data-using-php-to-display-gps-tagged-images-in-google-maps/
    function toDecimal($deg$min$sec$hemi) {
     
    $d $deg $min/60 $sec/3600; return ($hemi=='S' || $hemi=='W') ? $d*=-$d;
    }
    function 
    divide($a) {
     
    $e explode('/'$a);
     if (!
    $e[0] || !$e[1]) {
      return 
    0;
     } else {
      return 
    $e[0] / $e[1];
     }
    }
    function 
    getGPS() {
     global 
    $exif$lat_degrees$lat_minutes$lat_seconds$lat_hemi$log_degrees$log_minutes$log_seconds$log_hemi;
      if (
    $exif) {
      
    $lat $exif['GPS']['GPSLatitude'];   $log $exif['GPS']['GPSLongitude'];
      if (!
    $lat || !$log) return null;
      
    $lat_degrees divide($lat[0]);
      
    $lat_minutes divide($lat[1]);
      
    $lat_seconds divide($lat[2]);
      
    $lat_hemi $exif['GPS']['GPSLatitudeRef'];
      
    $log_degrees divide($log[0]);
      
    $log_minutes divide($log[1]);
      
    $log_seconds divide($log[2]);
      
    $log_hemi $exif['GPS']['GPSLongitudeRef'];
      
    $lat_decimal toDecimal($lat_degrees$lat_minutes$lat_seconds$lat_hemi);
      
    $log_decimal toDecimal($log_degrees$log_minutes$log_seconds$log_hemi);
      return array(
    $lat_decimal$log_decimal);
     } else {
      return 
    null;
     }
    }
    ?>
    gps_exif.css
    Code:
    body {
     margin: 0; 
     text-align: left; 
     background-image: url('background.jpg'); 
    }
    p { 
     font-family: Helvetica Neue, Geneva, Arial, Verdana; 
     font-size: 10px; 
     letter-spacing: 1px; 
     font-weight: normal; 
     color: #303030; 
     margin: 0; 
     text-align: left; 
    }
    #header {
     position: relative; 
     height: 50px; 
     width: 100%; 
     margin-top: 20px; 
     margin-bottom: 0; 
    }
    .headerbox {
     position: absolute; 
     width: 800px; 
     height: 50px; 
     top: 0; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     font-family: Helvetica Neue, Geneva, Arial, Verdana; 
     font-size: 36px; 
     letter-spacing: 1px; 
     font-weight: normal; 
     color: #f3f3f3; 
    }
    #image {
     position: relative; 
     height: 400px; 
     width: 100%; 
     margin-top: 20px; 
     margin-bottom: 0; 
    }
    .imagebox {
     position: absolute; 
     width: 800px; 
     height: 400px; 
     top: 0; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     background-image: url('box_image.gif'); 
    }
    .imageshow {
     float: left; 
     width:780px; 
     height:380; 
     margin-left: 10px; 
     margin-top: 10px; 
    }
    #exif {
     position: relative; 
     height: 260px; 
     width: 100%; 
     margin-top: 20px; 
     margin-bottom: 20px; 
     } 
    .exifbox {
     position: absolute; 
     width: 800px; 
     height: 260px; 
     top: 0px; left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     background-image: url('box_exif.gif'); 
    }
    .exifboxtable {
     position: absolute; 
     width: 800px; 
     height: 240px; 
     top: 20px; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     text-align: center; 
    }
    .exifboxtable table {
     width: 760px; 
     padding: 0; 
     border-collapse: collapse; 
     border: none; 
    }
    .exifboxtable td { width: 30%;  vertical-align: top; padding: 0; }
    .exifboxtable td+td+td { width: 40%; }
    img { border: none; }
    p { line-height: 2em; }
    gps_exif.php
    PHP Code:
    <?php 
     
    require_once "gps_exif_inc.php";
     
    $image = isset($img) ? $img ".jpg" null;
     
    readExif($image);
     
    ?> 
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>.: GPS EXIF :.</title>
    <link rel="stylesheet" href="gps_exif.css">
    </head>
    <body> 
    <div id="header"><div class="headerbox">GPS EXIF</div></div>
    <div id="image">
     <div class="imagebox">
      <div class="imageshow">
    <?php getImg($image); ?>
      </div>
     </div>
    </div>
    <div id="exif">
     <div class="exifbox">
      <div class="exifboxtable">
       <table>
        <tr>
         <td><p><strong>GPS </strong></p></td>
         <td><p><strong>GPS LONGDITUDE</strong></p></td>
         <td><p><strong>GPS LATITUDE</strong></p></td>
        </tr>
    <?php  print_data(); ?>
       </table>
      </div>
     </div>
    </div>
    </body>
    </html>
    Will add some comments later describing some of my thinking on this version.

    ---------- Post added at 11:17 PM ---------- Previous post was at 11:15 PM ----------

    The image name is passed in as a query string, gps_exif.php?img=image1 (no extension).
    Last edited by weegillis; 05-28-2012 at 03:24 AM. Reason: name

  2. #22
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793

    working draft two for gps_exif

    In this version I pared out everything that was not being reported to the display, so there is a lot less to wrestle with and no wasted clock ticks. I also switched to abbreviated underscore_case as a variable naming convention. camelCaseNames are nice, but better used for function names. First letter uppercase is generally reserved for class names in OOP.

    The idea was to free up the clutter and make everything easily discernible. One can pretty it up all one wants, but if it works and you understand its workings, there is no need for the extra verbiage. The key is to ensure that any programmer can work with the code, if they need to. And it makes your life easier, too.

    Simple, concise, brief is always best, as long as you can understand it six months or six years later.

    I'll start this time with the HTML portion as there is some explanation owed.

    PHP Code:
    <?php 
     
    require_once "gps_exif_inc.php"
     
    $image = isset($img) ? $img ".jpg" null
     
    readExif($image); 
     
    ?><!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>.: GPS EXIF :.</title>
    <link rel="stylesheet" href="gps_exif.css">
    </head>
    <body> 
    <div id="header"><div class="headerbox">GPS EXIF</div></div>
    <div id="image"><div class="imagebox"><div class="imageshow"><?php getImg($image); ?></div></div></div>
    <div id="exif">
     <div class="exifbox"> 
      <div class="exifboxtable">  
       <table>   
        <tr><th>GPS</th><th>LONGITUDE</th><th>LATITUDE</th></tr>    
    <?php print_data(); ?> 
       </table>  
       </div> 
      </div> 
    </div>
    </body>
    </html>
    The include file can be placed anywhere for easy site wide sharing. Keep the URL relative to the folder asking for it and it will work fine. I use require_once instead of include_once because it is unconditional. include_once is generally used only when it is the action of a condition.

    The image must be in the same folder as this page. Using your earlier discovery script, you could build a table then store it for retrieval in a series of pages using this one template. Then you could add previous/next navigation.

    The biggest change to this page is the headline. I switched over to TH, as they are headings. Styling the TH's allowed removal of all extraneous markup.

    I like to separate code from HTML from CSS as early as possible in a project; always thinking of portability and re-use. Ease of maintenance and debugging goes without saying. Something to adopt early in your programming exploits. It also saves you reinventing the wheel or creating multiple versions of essentially the same thing. Work toward building a code library; as well set up custom, section and site wide CSS, and template your pages so they can be re-used.

    Now the CSS, which is not much changed, but I had to work it a little bit. The images are commented out so I wouldn't be generating 404 responses with every refresh.
    Code:
    body {
     margin: 0; 
     text-align: left; 
    /* background-image: url('background.jpg'); */ 
    }
    p,
    .exifboxtable th { 
     font-family: Helvetica Neue, Geneva, Arial, Verdana; 
     font-size: 10px; 
     letter-spacing: 1px; 
     font-weight: normal; 
     color: #303030; 
     margin: 0; 
     text-align: left; 
    }
    p { 
     font-weight: normal; 
    }
    #header {
     position: relative; 
     height: 50px; 
     width: 100%; 
     margin-top: 20px; 
     margin-bottom: 0; 
    }
    .headerbox {
     position: absolute; 
     width: 800px; 
     height: 50px; 
     top: 0; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     font-family: Helvetica Neue, Geneva, Arial, Verdana; 
     font-size: 36px; 
     letter-spacing: 1px; 
     font-weight: normal; 
     color: #f3f3f3; 
    }
    #image {
     position: relative; 
     height: 500px; 
    }
    .imagebox {
     position: absolute; 
     width: 800px; 
     height: 500px; 
     top: 0; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
    /* background-image: url('box_image.gif'); */ 
    }
    .imageshow {
     float: left; 
     width:780px;
     margin-left: 10px; 
    }
    #exif {
     position: relative; 
     height: 260px; 
     width: 100%; 
    } 
    .exifbox {
     position: absolute; 
     width: 800px; 
     height: 260px; 
     top: 0px;
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
    /* background-image: url('box_exif.gif'); */ 
    }
    .exifboxtable {
     position: absolute; 
     width: 800px; 
     height: 240px; 
     top: 20px; 
     left: 50%; 
     margin-left: -400px; 
     z-index: 0; 
     overflow: hidden; 
     text-align: center; 
    }
    .exifboxtable table {
     width: 760px; 
     padding: 0; 
     border-collapse: collapse; 
     border: none; 
    }
    .exifboxtable td,
    .exifboxtable th { width: 30%; vertical-align: top; padding: 0; }
    .exifboxtable td+td+td,
    .exifboxtable th+th+th { width: 40%; }
    .exifboxtable th { font-weight: bold; line-height: 3em; }
    img { border: none; }
    p { line-height: 2em; }
    Last the include file, which as I mentioned, is pared down considerably.
    PHP Code:
    <?php 
    function readExif($img) {
     if (
    file_exists($img)) {
      global 
    $exif$gps_alt,$lat_dec$lon_dec;
      
    $exif read_exif_data($img0true);
      
    $gps_alt $exif["GPS"]["GPSAltitude"][0]; 
      
    $gps getGPS();
      if (
    $gps != null) {
       
    $lat_dec $gps[0];
       
    $lon_dec $gps[1];
      }
      
    normalizeSeconds();
     }
    }
    function 
    normalizeSeconds() {
     global 
    $lat_min$lat_sec$log_min$log_sec;
     
    $x = (int)$lat_min;
     
    $y = (int)$log_min;
     
    $lat_sec = ($lat_min $x) * 60;
     
    $log_sec = ($log_min $y) * 60;
     
    $lat_min $x;
     
    $log_min $y;
    }
    function 
    print_data() {
     global 
    $exif$gps_alt$lat_deg$lat_min$lat_sec$lat_hem$lat_dec$log_deg$log_min$log_sec$log_hem$lon_dec
      
    $str "    <tr>\n"
      
    $str .= "     <td>\n      <p>Altitude: $gps_alt</p>\n     </td>\n";  
      
    $str .= "     <td>\n      <p>Degrees: $lat_deg</p>\n      <p>Minutes: $lat_min</p>\n      <p>Seconds: $lat_sec</p>\n      <p>Latitude Hemisphere: $lat_hem</p>\n      <p>Decimal Latitude: $lat_dec</p>\n     </td>\n";
      
    $str .= "     <td>\n      <p>Degrees: $log_deg</p>\n      <p>Minutes: $log_min</p>\n      <p>Seconds: $log_sec</p>\n      <p>Longitude Hemisphere: $log_hem</p>\n      <p>Decimal Longitude: $lon_dec</p>\n     </td>\n";   
      
    $str .= "    </tr>";
      echo 
    $str;
    }
    function 
    getImg($img) {
     global 
    $exif;
     
    $fil_wid $exif['COMPUTED']['Width'];
     
    $fil_hgt $exif['COMPUTED']['Height'];
     if (
    file_exists($img)) {
      
    $str "<img src=\"$img\" width=\"$fil_wid\" height=\"$fil_hgt\" alt=\"" strTrunc($img,4) . "\">";
      echo 
    $str;
     } else {
      echo 
    "Image not found.";
     }
    }
    function 
    strTrunc($str,$trunc) { return substr($str,0,strlen($str)-$trunc); };
    // adapted from http://www.quietless.com/kitchen/extract-exif-data-using-php-to-display-gps-tagged-images-in-google-maps/
    function toDecimal($deg$min$sec$hem) {
     
    $d $deg $min/60 $sec/3600;
     return (
    $hem=='S' || $hem=='W') ? $d*=-$d;
    }
    function 
    divide($a) {
     
    $e explode('/'$a);
     if (!
    $e[0] || !$e[1]) {
      return 
    0;
     } else {
      return 
    $e[0] / $e[1];
     }
    }
    function 
    getGPS() {
     global 
    $exif$lat_deg$lat_min$lat_sec$lat_hem$log_deg$log_min$log_sec$log_hem
     if (
    $exif) {
      
    $lat $exif['GPS']['GPSLatitude']; 
      
    $log $exif['GPS']['GPSLongitude'];
      if (!
    $lat || !$log) return null;
      
    $lat_deg divide($lat[0]);
      
    $lat_min divide($lat[1]);
      
    $lat_sec divide($lat[2]);
      
    $lat_hem $exif['GPS']['GPSLatitudeRef'];
      
    $log_deg divide($log[0]);
      
    $log_min divide($log[1]);
      
    $log_sec divide($log[2]);
      
    $log_hem $exif['GPS']['GPSLongitudeRef']; 
      
    $lat_dec toDecimal($lat_deg$lat_min$lat_sec$lat_hem);
      
    $log_dec toDecimal($log_deg$log_min$log_sec$log_hem); 
      return array(
    $lat_dec$log_dec);
     } else {
      return 
    null;
     }
    }
    //
    ?>
    I could be dead wrong about the seconds normalization, but it was bugging me that minutes had a decimal fraction and seconds were always zero. The function can be dropped by simply not calling it. (comment out the function call line). Unfortunately it goes under the assumption that seconds will ALWAYS be zero and simply replaces that value with the computed normalized value for the minutes decimal fraction. It is accurate in this sense.

    This should give you something to work with. Questions are always welcome.

    ---------- Post added at 03:38 AM ---------- Previous post was at 02:15 AM ----------

    You do notice I don't say, "// adapted from..." on your code? If I look around where am I going to find the code you've started me with? You do realize I know how to search?

    ---------- Post added at 04:01 AM ---------- Previous post was at 03:38 AM ----------

    Piping in one last comment, the use of first letter uppercase for CSS selectors is not a recommended practice. The first time you forget a capital first letter your page could break and you'd be searching for hours for the cause. Stick to lowercase or underscore_lowercase and save yourself and any other programmer much grief.
    Last edited by weegillis; 05-28-2012 at 05:35 AM. Reason: the -> to / it .. me

  3. #23
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    Funny, this was just posted on StackOverflow yesterday:

    http://stackoverflow.com/questions/1...conversion-php

    Exact same script we started with, and same question, too.

    I'm glad for one thing, at least I learned something this weekend. Don't know if anybody else did, though.

    Update: After reviewing the StackOverflow page, a week later, it is only too clear the OP did not learn a thing from this thread, as evidenced by the link he posted: Easy PHP EXIF. I hope this member never returns. He'll get no more help from me, that's certain.

    ---------- Post added at 02:14 PM ---------- Previous post was at 02:03 PM ----------

    It just gets curioser and curiouser...

    http://phpmagister.com/gps-co-ordina...ion-php-25084/

    Again, identical to the StackOverflow post. And, posted just yesterday.
    Last edited by weegillis; 06-02-2012 at 04:23 PM. Reason: update

  4. #24
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    For the complete solution and the resulting application, visit this thread: Extracting and displaying EXIF data with PHP.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •