Submit Your Article Forum Rules

Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Php script problem.

  1. #11
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    Without studying it much further I've concluded the GPS data should not be stripped out, but the exif collection should be accessible to the (converter) function, where it can query the keys it needs. We just need to add the collection to the global scope so the function can see it. Then we don't need any parameter. The returns would contain the decimal data you need for your display.

    Consequently, the two bulky strings in the above code (GPS) could be replaced with the a couple of function calls, or maybe even one. Would really simplify the remaining, first time around code.

    I'm also thinking that the above code repeats echo with every iteration through the jpg collection. If there are several photos, we're back to the same issue with echo. The full loop for all jpg finds can be concatenated onto the base string, and the whole shooting match kicked out in one echo statement. Just null out the string variable BEFORE the foreach() loop, not inside it (or you'll lose all but the last iteration). This will give the CPU clicks for the iterations, rather than the echoes.

    Still studying. Haven't actually run the code yet, but I have about 2 GB of photos off my brother's iPhone to play with. Will have to mount some and see what I resolve at my end. I will need your thumbnail.php file, too, so PM me if you don't want to post it here, please.
    Last edited by weegillis; 05-26-2012 at 06:01 AM. Reason: most? > move /(..) /extraneous text removed

  2. #12
    Banned
    Join Date
    May 2012
    Posts
    10
    No problem,
    here's the script - it's very small

    <?php
    // define directory path
    $dir = ".";
    $image = exif_thumbnail($dir . "/" . $_GET['file']);
    header("Content-Type: image/jpeg");
    echo $image;
    ?>

  3. #13
    Banned
    Join Date
    May 2012
    Posts
    10
    Here is a bit more background I've come up with

    Content of the GPS Latitude and GPS Longitude fields in photograph EXIF data and the required formats.

    There are 4 fields for each (Lat and Lon) which I retrieve as follows in a php script

    // --- Get GPS degrees latitude --- //
    $GPSDegreesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][0]);
    // --- Get GPS minutes latitude --- //
    $GPSMinutesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][1]);
    // --- Get GPS seconds latitude --- //
    $GPSSecondsLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][2]);
    // --- Get GPS Hemisphere for latitude --- //
    $GPSSecondsLat2 = ($ReadEXIFData["GPS"]["GPSLatitudeRef"]);

    The same info is used for Longitude (replacing relevant words).

    The results return the following (using Latitude as an example)

    51/1 - Degrees
    29/1 - Minutes
    20649/2411 - Seconds
    N - Hemisphere (North or south, and East or West for Longitude)

    Apparently the figures above represent an equation so the resulting co-ordinate is
    N 51 Degrees 29 Minutes 8.56 Seconds

    So my first consideration is how to return the result by dividing what comes before the '/' by what follows it.

    My second consideration is to the convert this calculated co-ordinate from degress to decimal.

    The equation for this would be
    A+B+C
    where
    C is the seconds divided by 3600
    B is the minutes divided by 60
    A is the Degrees

    (The same calculations apply to the Longitude)

    Finally, the decimal answer has to be negative if the Hemisphere is S or W and positive if N or E.
    So what it boils down to (I think) is finding a way to
    divide what lies in front of the '/' by what follows it, then in turn dividing the results by 3600 or 60 or 1 and adding them together, then applying a '-' if the hemisphere is S or W.

  4. #14
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    Here is the GPS portion of the EXIF array dump:

    PHP Code:
     [GPS] => Array (
      [
    GPSLatitudeRef] => N,
      [
    GPSLatitude] => Array (
       [
    0] => 49/1,
       [
    1] => 1700/100,
       [
    2] => 0/
      
    )
      [
    GPSLongitudeRef] => W,
      [
    GPSLongitude] => Array (
       [
    0] => 123/1,
       [
    1] => 625/100,
       [
    2] => 0/
      
    )
      [
    GPSAltitudeRef] => [GPSAltitude] => 15136/339,
      [
    GPSTimeStamp] => Array (
       [
    0] => 13/1,
       [
    1] => 51/1,
       [
    2] => 5631/
      
    )
      [
    GPSImgDirectionRef] => T,
      [
    GPSImgDirection] => 72277/234
     

    The overall solution will end up being very simple, but I'm unable to concentrate on this problem right now, and want to spend the weekend mulling it over. Hope you can be patient. If not, let me know what you come up with and we can work on refining the whole thing into a couple of nice functions that can be re-used site wide.

    I have mounted a couple of iPhone pictures and the above HTML5 page/script so I have my own working version, now. That's how I created the array dump (see EXIF thread for full dump).
    Last edited by weegillis; 05-25-2012 at 05:52 PM.

  5. #15
    Banned
    Join Date
    May 2012
    Posts
    10
    No problem WG.

    I can wait - I'm just grateful for the assistance

  6. #16
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793

    Post Working draft

    Okay, I've adapted the OOP script reference mentioned earlier and have a working version. It is still 'in page PHP' but over the weekend I'll separate out the PHP and store it in a library file. This version returns LAT and LON as degrees + decimal degrees.
    PHP Code:
    <?php 
    // 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;
     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;
     }
    }

    function 
    getEXIF($dir) {
     global 
    $exif; if (is_dir($dir)) {
      if (
    $dh opendir($dir)) {
       while ((
    $file readdir($dh)) !== false) {
        if (
    stristr($file,".jpg")) {
         
    $exif exif_read_data($file0true);
         
    $str " <tr>\n";
         
    $str .= "  <td><div><a href=\"$dir/$file\"><img src=\"thumbnail.php?file=$file\"></a></div></td>\n";
         
    $str .= "  <td><div>";
         
    $str .= "File: <b>" $exif['FILE']['FileName'] . "</b><br>\n";
         
    $str .= "Timestamp: " $exif['IFD0']['DateTime'] . "<br>\n";
         
    $str .= "Dimensions: " $exif['COMPUTED']['Width'] . " x " $exif['COMPUTED']['Height'] . " <br>\n";
         
    $str .= "Camera: " $exif['IFD0']['Model'] . "<br>\n";
         
    $gps getGPS();
         if (
    $gps != null) {
          
    $str .= "Latitude: " $gps[0] . "<br>\n";
          
    $str .= "Longitude: " $gps[1] . "<br>\n";
         }
         
    $str .= "\n  </div></td>\n";
         
    $str .= " </tr>\n";
         echo 
    $str;
        }
       }
       
    closedir($dh); 
      }
     }
    }
     
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Thumbnail page with EXIF data</title>
    <style>
    body { font-size: 100%; }
    td { font-size: 0.8em; }
    td * { font-size: 100%; }
    </style>
    </head>
    <body>
    <table>
    <?php $imgDir "."getEXIF($imgDir); ?>
    </table>
    </body>
    </html>
    Last edited by weegillis; 05-25-2012 at 09:49 PM. Reason: add if ($gps) condition / removed lat-lon refs

  7. #17
    Banned
    Join Date
    May 2012
    Posts
    10
    GW, that's great - and so quick.

    It works perfectly.

    Without being too much of a nuisance can I ask you to look at the attached file.
    It does a similar thing, but in a different way, and just for one image, which at the moment is hard coded.

    Although I understand the logic of your code for convesrion I could not figure quie how to use it in the attached file.
    Attached Files Attached Files

  8. #18
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    It's quick because it doesn't waste time with echoes. Go back to the original and time it, then time this one. I bet we shaved off two thirds of the time. It pays always to find out how CPU intensive your scripts are. Always test and benchmark until you become confident in your methods; and, don't be so proud you will not adopt others. That's been my saving grace.

    ---------- Post added at 03:06 AM ---------- Previous post was at 02:53 AM ----------

    Work on it for a couple of days, and if you still can't figure it out, then give me a rattle. You have work to do. I've done mine.

  9. #19
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    Quote Originally Posted by pewe View Post
    GW, that's great - and so quick.

    It works perfectly.

    Without being too much of a nuisance can I ask you to look at the attached file.
    It does a similar thing, but in a different way, and just for one image, which at the moment is hard coded.

    Although I understand the logic of your code for convesrion I could not figure quie how to use it in the attached file.
    The general principle in programming is to avoid repetition of code, which is why it is a good practice to design your code for re-use, and thus not 'in page.' Sure it's okay to copy and paste the same snippet into several pages, but why?

    For the purpose of testing, one can see that working in a single page makes things easier to tie together, but it can get messy sometimes. Just as we have begun to do in the above working version, one will hope you would do in this case as well, which is separate out the PHP methods and store them in a library file which can be merged into any page that needs them. I won't beleaguer this point, it's been stressed enough.

    I'm looking at the code, but haven't run it. Will refine it to my liking and test it then. Some comments:

    1) Recommend always include a DOCTYPE in your HTML. <!doctype html> will suffice in today's world. If all you're using is HTML 4 tags, it will still work in any browser, no matter how old.

    2) Since you've got a style sheet, why not add rules to accommodate width, align, and border attribute properties. For instance, <img src="" border="0" />, in HTML5 can be written as, <img src="" alt="">. Yes, don't forget the alternate text, even if it is just a copy of the image file name (without the mime extension, of course). This way if the image fails to load, you still have replacement text.
    Code:
    CSS
    
    td { width: 30%; vertical-align: top; }
    td+td+td { width: 40%; }
    img { border: none; }
    p {line-height: 2em; }
    Can be added to your main style sheet, and all attributes removed from the source markup.

    3) Again, is there an echo in here? Being an i/o process, and not a function, ECHO takes lots of time and loads of CPU ticks. A string is usually the best and fastest way to output.

    4) Lots of linebreaks (<br />) which can be removed, and emulated using the CSS line-height property as shown above. Of course one will want to tweak this for best appearance, but it eliminates the need for added superfluous markup. <br /> can be written as <br> in HTML5, though it is not a sticking point. Both XHTML and HTML are valid HTML5, for the most part. One uses what one is used to. I've switch back to HTML 4 markup style in my HTML5, but not deprecated or obsolete tags from the olden days. Those that were invalid in XHTML are still, in my mind, invalid in HTML5, even if this is not actually the case.

    Still working on this. Will get back later with my results.
    Last edited by weegillis; 05-27-2012 at 02:55 AM. Reason: s/g

  10. #20
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,793
    5) I'm back into it and just looked at the style sheet. Ugh.

    CSS shorthand, and units on zero:

    top-right-bottom-left, in that order, is the way to create shorthand rules for multiple parameters sharing the same property, as in margin. We can disregard the parameters if their values are all the same.

    Eg. p { margin: 0; } says to set all paragraphs' top-right-bottom-left margins to zero, without all the verbiage.

    Zero is undefined, so how can it have units? Think on it. Zero is just 0. No units.

    ---------- Post added at 12:39 AM ---------- Previous post was at 12:31 AM ----------

    On #4 above I should add that the only reason I bring this up is because you are already using P's, wherein BR is superficial, and geared to specific text formatting within the P block. The only block level element allowed INSIDE a P is a BR. Outside of a P, BR becomes superfluous. The same spacing can be handled by the style sheet.
    Last edited by weegillis; 05-27-2012 at 02:56 AM. Reason: s/g

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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