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;

// --- 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).

// --- 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:

// --- 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

Almost 100 views and no one has answered. :frowning:

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

Here is what I think you need:

Your first script should be something like this:

<?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($Image, 0, true);
   $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
   $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*=-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;
	   }
   }

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

?>

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

Also note that I named the second script exif.php.

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 :confused: - but it did:)

Could have been a scope issue. As you didn’t show us the combined code, we can’t tell what problem you had.

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):wink:

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.