SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with a script when it is combined with another.

    First let me say that although I have put together various web pages/functions in the past by 'hacking' 3rd party scripts to suit my needs, I am not a coder or programmer.

    I recently took up photography and rather than use one of the proprietary photo sites I am currently slowly putting together a personal web site for uploading/storing/displaying potographs.

    I have a script for the upload which uploads (one at a time) photographs then resizes it to 2 different sizes and stores all 3 photos in specified (in the code) directories.

    The second script was developed to open a photograph (name and location hard coded) extract exif data from the photograph and display the image and data on a web page.

    In this script I had to add a calculation to display 2 of the fields in a particular way. Just for info, the fields are the GPS co-ordinates for latitude/longitude which are stored in the photograph as Degrees/Minutes/Seconds and I need to display them in decimal.

    Now here is where I have run into a problem.

    I combined the 2 scripts so that when the upload is complete the second script kicks in and displays the info for the original sized image. (The next step will be to place that info in a SQL database.)
    The combined script works in principle, but what is missing is the calculated information.

    The only change I made to the second script (which is called as an 'include' by the firs)t was to the variable specifying the image name and its location.

    I changed this code;
    Code:
    // --- Change the 'image1.jpg' value below to the path of your file. --- //
    
    
    $Image = 'original/test.jpg';
    
    // --- Read EXIF Data --- //
    
    $exif = exif_read_data($Image, 0, true);
    to this: ($Image99 is a variable set in the upload script to specify the name and location of the uploaded image).

    Code:
    // --- Change the 'image1.jpg' value below to the path of your file. --- //
    
    
    $Image = $Image99;
    
    // --- Read EXIF Data --- //
    
    $exif = exif_read_data($Image, 0, true);
    The code which works in the stand alone script but fails in the combined one is this:

    Code:
    // --- The EXIF GPS Conversion Degrees to Decimal --- //
    
    function toDecimal($deg, $min, $sec, $hemi) {
     $d = $deg + $min/60 + $sec/3600;
     return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $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;
     }
    }
    
     $gps = getGPS();
         
    // --- end EXIF GPS Conversion Degrees to Decimal --- //

    What I fail to understand is why the second script works fully as a stand alone, but misses info when 'included'.

    I have tried to find a solution elsewhere with no success, and am hoping someone of experience here can tell me how I could go about doing an analysis to determine where the problem is arising (remembering I have little experience in this area at this time).

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost 100 views and no one has answered.

    Is there no one can suggest a way of finding the problem - or where I may get further info/help in doing so?

  3. #3
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here is what I think you need:

    Your first script should be something like this:

    PHP Code:
    <?php
       
    include('exif.php');
       
    // --- Change the 'image1.jpg' value below to the path of your file. --- //

       
    $Image $Image99;

       
    // --- Read EXIF Data --- //

       
    $exif exif_read_data($Image0true);
       
    $gps getGPS();
    ?>
    Remove $gps = getGPS(); from your second script. Note that it is added into the first script.

    Your second script should be something like this:

    PHP Code:
    <?php
       $Image99 
    'original/test.jpg';
       
       
    // --- The EXIF GPS Conversion Degrees to Decimal --- //

       
       
    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;
           }
       }

    // --- end EXIF GPS Conversion Degrees to Decimal --- //

    ?>
    If $Image99 is defined elsewhere then remove it from the second script.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Also note that I named the second script exif.php.

  5. #5
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tom, thanks for the input - but I managed to resolve the problem by simply moving the include statement to a different position in the upload script.

    Don't understand why that made a difference - but it did

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Could have been a scope issue. As you didn't show us the combined code, we can't tell what problem you had.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could have been a scope issue.
    Jake, can you explain (or point me to somewhere that may explain) what that means so I can investigate - (out of interest more than a need to re-solve the problem)

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I didn't see your use of the global keyword in the second script, so scope actually wouldn't be an issue. However, truthfully, you should modify the initial function to take exif as a parameter rather than using global.

    It's just a case of making sure the variable you want is available.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

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