Submit Your Article Forum Rules

Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Php script problem.

  1. #1
    Banned
    Join Date
    May 2012
    Posts
    10

    Php script problem.

    I have no experience as a coder/programmer and wonder if someone here can help.

    I have a script (about 2 years old) which was written by someone who is no longer contactable.

    The script is used to extract data from photographs using the PHP exif functions.

    The script consists of 2 parts 'gallery.html' (includes php code) and 'thumbnail.php', and this should read the image data and display it alongside thumbnails of all the images in a directory.

    When I run the scrip it displays the following in a web page instead of the thumbnails/data

    "; // get thumbnail // link to full image echo ""; echo ""; } } closedir($dh); } } ?>
    "; echo " "; // get file name echo "File: " . $exif['FILE']['FileName'] . "
    "; // get timestamp echo "Timestamp: " . $exif['IFD0']['DateTime'] . "
    "; // get image dimensions echo "Dimensions: " . $exif['COMPUTED']['Height'] . " x " . $exif['COMPUTED']['Height'] . "
    "; // get camera make and model echo "Camera: " . $exif['IFD0']['Model']; echo "
    So something is wrong in the code, can anyone enlighten me as to what?

    Copies of the code are below

    gallery.php:

    <html>
    <head></head>
    <body>
    <table>
    <?php
    // define directory path
    $dir = ".";

    // iterate through files
    // look for JPEGs
    if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
    if (preg_match("/.jpg/", $file)) {
    // read EXIF headers
    $exif = exif_read_data($file, 0, true);
    echo "<tr>";
    // get thumbnail
    // link to full image
    echo "<td valign=top><a href=$dir/$file><img src=thumbnail.php?file=$file></a><td>";
    echo "<td valign=top><font size=-1>";
    // get file name
    echo "File: <b>" . $exif['FILE']['FileName'] . "</b><br/>";
    // get timestamp
    echo "Timestamp: " . $exif['IFD0']['DateTime'] . "<br/>";
    // get image dimensions
    echo "Dimensions: " . $exif['COMPUTED']['Height'] . " x " . $exif['COMPUTED']['Height'] . " <br/>";
    // get camera make and model
    echo "Camera: " . $exif['IFD0']['Model'];
    echo "</font></td>";
    echo "</tr>";
    }
    }
    closedir($dh);
    }
    }
    ?>
    </table>
    </body>

    thumbnail.php:

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

  2. #2
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,785
    Just quickly tossing this one out while I continue to peruse the script:

    PHP Code:
     $dir =  getcwd(); 
    is far more explicit than what you have there. They might be the same, in actuality, but the difference is well apparent, also. That's unless I'm reading this wrong. "." is the current directory, right?

    In this light, it makes little sense to test if the directory exists, as you're in it. The only time we would need to verify the existence of a directory is if the name came down as data either stored or included in a query.

    Back to my reading...

    ---------- Post added at 06:50 PM ---------- Previous post was at 06:45 PM ----------

    Some of the code is right out of here: http://php.net/manual/en/function.opendir.php. Notice they have a 'named' directory, not a server default. I fail to understand why the programmer would verify when it is not named. Need to figure that one out...

    ---------- Post added at 06:56 PM ---------- Previous post was at 06:50 PM ----------

    Continuing...

    preg_match is a resource waste when the string is known. ".jpg" is a string constant, so stristr() is a better function to use. It is very quick, and doesn't burn clock ticks.

    Reading...

    ---------- Post added at 07:02 PM ---------- Previous post was at 06:56 PM ----------

    Next comment...

    Is there an echo in here? Those nine echo statements probably take 90 times longer to execute than one echo of a big, long string or thousands of characters. Well, I may be exaggerating, but it is known that echo is a huge resource hog, and very slow. The preferred method is to build a string of your HTML, then pass the whole thing off to echo.

    Still studying...

    ---------- Post added at 07:18 PM ---------- Previous post was at 07:02 PM ----------

    Lets look at how we can transform this 'mess' into something reliable...
    PHP Code:
        echo "<tr>";
    // get thumbnail
    // link to full image
        
    echo "<td valign=top><a href=$dir/$file><img src=\"thumbnail.php?file=$file\"></a><td>";
        echo 
    "<td valign=\"top\"><font size=\"-1\">";
    // get file name
        
    echo "File: <b>" $exif['FILE']['FileName'] . "</b><br/>";
    // get timestamp
        
    echo "Timestamp: " $exif['IFD0']['DateTime'] . "<br/>";
    // get image dimensions
        
    echo "Dimensions: " $exif['COMPUTED']['Height'] . " x " $exif['COMPUTED']['Height'] . " <br/>";
    // get camera make and model
        
    echo "Camera: " $exif['IFD0']['Model'];
        echo 
    "</font></td>";
        echo 
    "</tr>"
    First, I have to comment on the sloppiness of this code, documented or not. I'm going to strip all the comments away so we can see the real meat. I say sloppy because while the programmer pretends this to be XHTML code with the <br/> tags (which it is not as we see 'valign' attributes and 'font' tags, and in XHTML it should be <br /> to be valid) he leaves out the quotes on attributes. Very sloppy. It's as well, in my opinion that you can't reach him--it's not worth the time or effort.

    Notice anything amiss, here...
    PHP Code:
        echo "Dimensions: " $exif['COMPUTED']['Height'] . " x " $exif['COMPUTED']['Height'] . " <br/>"
    Where is the Width parameter?

    Let's strip the comments:
    PHP Code:
        echo "<tr>";
        echo 
    "<td valign=top><a href=$dir/$file><img src=thumbnail.php?file=$file></a><td>";
        echo 
    "<td valign=top><font size=-1>";
        echo 
    "File: <b>" $exif['FILE']['FileName'] . "</b><br/>";
        echo 
    "Timestamp: " $exif['IFD0']['DateTime'] . "<br/>";
        echo 
    "Dimensions: " $exif['COMPUTED']['Height'] . " x " $exif['COMPUTED']['Height'] . " <br/>";
        echo 
    "Camera: " $exif['IFD0']['Model'];
        echo 
    "</font></td>";
        echo 
    "</tr>"
    Now let's replace echo with a '$str .= ' expression opener and add in the missing quotes and a newline to prettify the output. Note the closed TD tag.
    [php]

    PHP Code:
     $str "<tr>\n";
     
    $str .= "<td valign=\"top\"><a href=\"$dir/$file\"><img src=\"thumbnail.php?file=$file\"></a></td>\n";
     
    $str .= "<td valign=\"top\"><font size=\"-1\">";
     
    $str .= "File: <b>" $exif['FILE']['FileName'] . "</b><br>";
     
    $str .= "Timestamp: " $exif['IFD0']['DateTime'] . "<br>";
     
    $str .= "Dimensions: " $exif['COMPUTED']['Width'] . " x " $exif['COMPUTED']['Height'] . " <br>";
     
    $str .= "Camera: " $exif['IFD0']['Model'];
     
    $str .= "</font></td>\n";
     
    $str .= "</tr>\n";
     echo 
    $str
    Give this a whirl and see what happens.

    ---------- Post added at 07:30 PM ---------- Previous post was at 07:18 PM ----------

    Last comment...

    This should be updated to more recently acceptable HTML, without 'valign' and 'font' and the the TD should not be the content container, but contain the content container, such as a P or DIV. Just a good practice.

    ---------- Post added at 07:42 PM ---------- Previous post was at 07:30 PM ----------

    Very last comment...

    A full path to the 'thumbnail.php' is preferred. You'll nail it, every time, and only need one copy for the whole site. Same goes for gallery.php. It could be re-used by the entire site.
    Last edited by weegillis; 05-26-2012 at 06:08 AM. Reason: really messed up attribute and tag

  3. The following user agrees with weegillis:
  4. #3
    Banned
    Join Date
    May 2012
    Posts
    10
    Weegillis:

    I can't thank you enough for your assitance - you went futher than I expected you to have the time for.

    Having said that, I wonder if you'd have the time to consider a further issue with the script.

    Here is the full script I ended up with

    <html>
    <head></head>
    <body>
    <table>
    <?php
    // define directory path
    $dir = ".";

    // iterate through files
    // look for JPEGs
    if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
    if (preg_match("/.jpg/", $file)) {
    // read EXIF headers
    $exif = exif_read_data($file, 0, true);
    $str = "<tr>\n";
    $str .= "<td valign=\"top\"><a href=\"$dir/$file\"><img src=\"thumbnail.php?file=$file\"></a></td>\n";
    $str .= "<td valign=\"top\"><font size=\"-1\">";
    $str .= "File: <b>" . $exif['FILE']['FileName'] . "</b><br>";
    $str .= "Timestamp: " . $exif['IFD0']['DateTime'] . "<br>";
    $str .= "Dimensions: " . $exif['COMPUTED']['Width'] . " x " . $exif['COMPUTED']['Height'] . " <br>";
    $str .= "Camera: " . $exif['IFD0']['Model'] . "<br>";
    $str .= "Latitude: " . $exif["GPS"]["GPSLatitudeRef"] . " " . $exif['GPS']['GPSLatitude'][0] . "-" . $exif['GPS']['GPSLatitude'][1] . "-" . $exif['GPS']['GPSLatitude'][2] . "<br>";
    $str .= "Longitude: " . $exif["GPS"]["GPSLongitudeRef"] . " " . $exif['GPS']['GPSLongitude'][0] . "-" . $exif['GPS']['GPSLongitude'][1] . "-" . $exif['GPS']['GPSLongitude'][2];
    $str .= "</font></td>\n";
    $str .= "</tr>\n";
    echo $str;
    }
    }
    closedir($dh);
    }
    }
    ?>
    </table>
    </body>
    As you can see I have found some info enabling me to include the GPS co-ordinates of the image from the GPSLatitude and GPSLongitude contained in the exif data.

    The results can be seen here www(dot)ukf(dot)com/gpx/exif/gallery2.php (sorry I'm not allowed to post links)

    The problems are:

    - the resulting co-ordinates have a trailing /*,
    - and are shown in Degrees - whereas I would also like them shown as decimal.

    I have found a reference to this here

    ishankarve(dot)blogspot(dot)co(dot)uk/2011/02/code-sample-convert-gps-nmea-lat-long.html

    but have no idea where to start trying to put this into my script.

    Any thoughts you may have on this and would care to share would be much appreciated.

  5. #4
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,785
    First, let's start with some clean, undocumented code. Notice that I swapped out 'preg_match()' with 'stristr()', the much faster function which is more appropriate for this application. Try dropping this in so we can look at the output, again, and go from there. We'll get to the converter when the GPS data is outputting correctly, since we can't do anything until then. Notice that we switched up to a more modern document type (HTML5) and removed the deprecated or obsolete attributes and tags in favor of a style sheet.

    PHP Code:
    <!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; vertical-align: top;}
    </style>
    </head>
    <body>
    <table>
    <?php 
    $dir 
    ".";
    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>";
         
    $str .= "Latitude: " $exif["GPS"]["GPSLatitudeRef"] . " " $exif['GPS']['GPSLatitude'][0] . "-" $exif['GPS']['GPSLatitude'][1] . "-" $exif['GPS']['GPSLatitude'][2] . "<br>\n";
         
    $str .= "Longitude: " $exif["GPS"]["GPSLongitudeRef"] . " " $exif['GPS']['GPSLongitude'][0] . "-" $exif['GPS']['GPSLongitude'][1] . "-" $exif['GPS']['GPSLongitude'][2];
         
    $str .= "\n  </div></td>\n";
         
    $str .= " </tr>\n";
         echo 
    $str;
        }
       }
       
    closedir($dh);
      }
     }
     
    ?>
    </table>
    </body>
    </html>
    Below is the sample snippet borrowed from here:
    code-sample-convert-gps-nmea-lat-long

    PHP Code:
    function degree2decimal($deg_coord) {
     
    $degree = (int)($deg_coord/100);
     
    $minutes $deg_coord - ($degree*100);
     
    $dotdegree $minutes/60;
     
    $decimal $degree $dotdegree;
     
    $direction substr($deg_coord,-1);
     if ((
    $direction == "S") or ($direction == "W")) { $decimal $decimal*(-1); }
     
    $decimal number_format($decimal,4,'.','');
     return 
    $decimal;



    You will want to be able use these methods in any folder on the site so it be advisable to create a library file that can be included in pages that need it. We can discuss this further, when other issues are solved.


    Last edited by weegillis; 05-25-2012 at 05:53 PM. Reason: removed extraneous message re: hijack

  6. #5
    Banned
    Join Date
    May 2012
    Posts
    10
    Thanks WG.

    The revised file is on site in gallery4.php

    It is working as before - but I do notice that the font for the File name for some reason has changed to a different size to the rest - no biggie, but puzzling.

    Not sure if you wanted me to put the decimal conversion in, and if so where.

  7. #6
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,785
    I'm not able to connect to your domain, or the page www.ukf.com/gpx/exif/gallery4.php. Is your site down?

    The font size can be sorted out in the CSS. I just threw in a couple of rules that can be tweaked.

    The decimal converter is a standalone method that will get called from within the main method. We will get to that in time.
    Last edited by weegillis; 05-24-2012 at 06:09 PM.

  8. #7
    Banned
    Join Date
    May 2012
    Posts
    10
    Seems to be - sometimes it goes off line for a short while -about once a month and usually late at night - and it's 11.00pm here in the UK

    ---------- Post added at 11:12 PM ---------- Previous post was at 11:11 PM ----------

    It's back up

    ---------- Post added at 11:20 PM ---------- Previous post was at 11:12 PM ----------
    Quote Originally Posted by juto
    I have a simple question, I have googled but found no simple answer. What I need is a php script that removes all exif data during image upload.

    Juto

    I have started a new thread for you here (with a possible solution)

    It's entitled - Removing Exif data from images on server

    (sorry can't post links here yet)

    No problem with you coming in here, but if you have your own thread it keeps answers clearer and tidier.
    Last edited by weegillis; 05-25-2012 at 05:55 PM. Reason: Repairing thread

  9. #8
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,785
    While studying your problem, I came across this OOP, "Extract EXIF data using PHP". This script hints at "/" in the very last function. Might need to experiment with some of the logic being used. It's not copy paste code, being object oriented, but the logic is there.

    Your style sheet can be adjusted to get a uniform font:

    Code:
    td { font-size: 0.8em; }
    td * { font-size: 100%; }
     /*... OR ...*/ 
    td div * { font-size: 100%; }
    Keep the first rule above the second so the 80% size is inherited by the div. The 100% size in the second rule is 'relative' to the size in the TD rule.
    Last edited by weegillis; 05-25-2012 at 02:46 AM. Reason: the

  10. #9
    Banned
    Join Date
    May 2012
    Posts
    10
    Thanks for the css - that did it.

    For the conversion I found this

    stackoverflow[dot]com/questions/2526304/php-extract-gps-exif-data

    and this

    en[dot]wikipedia[dot]org/wiki/Geotagging

    Do these make sense?

  11. #10
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,785
    Quote Originally Posted by pewe View Post
    Thanks for the css - that did it.

    For the conversion I found this

    stackoverflow[dot]com/questions/2526304/php-extract-gps-exif-data

    and this

    en[dot]wikipedia[dot]org/wiki/Geotagging

    Do these make sense?
    The CSS should look like this:
    Code:
    td { font-size: 0.8em; }
    td * { font-size: 100%; }
    Not like you have it. The first rule should be applied only to the TD, the second rule is applied to everything INSIDE the TD, AFTER it inherits the base size from the TD. This will tend to bring down the size to what you had. Right now, it is 100%, which matches the base size adopted in the BODY.

    I've been able to make sense out the examples you gave, but am still unsure the makeup of the co-ordinates as they are passed to the function. Another day or two and I should understand well enough. I think you've got a handle on it, now, though. Still need to separate the PHP from the HTML page, though, and let it be sharable across the site.
    Last edited by weegillis; 05-25-2012 at 02:48 AM. Reason: to

Page 1 of 3 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
  •