ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Alastair Gordon on February 02, 2023, 11:36:47 PM

Title: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 02, 2023, 11:36:47 PM
I have just started using exiftool, and it seems like an amazing product (thanks, Phil). It works flawlessly for extracting and displaying EXIF data, but I also need to strip GPS data from the EXIF metadata in JPG files. I followed Phil's instructions, but I seem unable to create a new JPG file with the GPS data removed. I have tried:

eval(`exiftool -gps:all= image.jpg`);
eval(`exiftool -gps:all= -overwrite_original image.jpg`);

but neither overwrite the target file with GPS data removed. I admire the difficulty of overwriting existing files, but I seem unable to get around it and update my jpg files. Perhaps my ignorance of eval in PHP is part of the problem. Any suggestions are much appreciated. I am definitely a newbie.

Website in Apache running on Windows 10.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 03, 2023, 01:32:53 AM
Test your commands on the command line before trying to call them from another script.

Use FAQ #3 (https://exiftool.org/faq.html#Q3) to verify the locations of the GPS tags.  They may show up in the XMP group as well.  Composite tags (https://exiftool.org/TagNames/Composite.html) can be ignored, as they are not actually embedded in the file.

Example of the most common locations.
C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg
[GPS]           GPSVersionID                    : 2.3.0.0
[GPS]           GPSLatitudeRef                  : North
[GPS]           GPSLatitude                     : 40 deg 41' 21.12"
[GPS]           GPSLongitudeRef                 : West
[GPS]           GPSLongitude                    : 74 deg 2' 40.20"
[GPS]           GPSAltitudeRef                  : Above Sea Level
[GPS]           GPSAltitude                     : 10 m
[XMP-exif]      GPSAltitude                     : 10 m
[XMP-exif]      GPSAltitudeRef                  : Above Sea Level
[XMP-exif]      GPSLatitude                     : 40 deg 41' 21.12" N
[XMP-exif]      GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]     GPSAltitude                     : 10 m Above Sea Level
[Composite]     GPSLatitude                     : 40 deg 41' 21.12" N
[Composite]     GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]     GPSLatitudeRef                  : North
[Composite]     GPSLongitudeRef                 : West
[Composite]     GPSPosition                     : 40 deg 41' 21.12" N, 74 deg 2' 40.20" W

Finally, depending upon what programs you use, the data may be in an XMP sidecar file, which will have the same name as the image except with a .xmp extension.   This is most common with RAW file types such as NEF/CR2.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 03, 2023, 09:03:58 AM
Thanks, StarGeek, but I still do not know the code I need to simply strip the gps data and then write the modified content, either to the same jpg file, or to another file. Exiftool has worked perfectly for me for capturing and displaying exif data on my website. The problem is exclusively with stripping location data from a jpg file and saving the new GPS-less content into the same or a different file. Please remember my newbie status (that hopefully is diminishing every day). Thanks again.

By the way, do I get an email notification when a response is posted? I'm also a newbie on this forum.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Phil Harvey on February 03, 2023, 09:20:07 AM
Hi Alastair,

You can click on the "No Alerts or Emails" button at the bottom of the posts to change this to get alerts if you want.  (I think the forum emails are working presently.)

There are many ways to do what you want, but I would suggest something like this:

exiftool -location:all= FILE

This should delete GPS tags as well as other location-specific information such as City, Country, etc.

Here is another option (as mentioned by StarGeek).  This command deletes any tag with a name starting with "GPS":

exiftool "-gps*=" FILE

These commands and the commands that you mentioned (-gps:all=) should all overwrite the target file, with the original file being preserved with an "_original" suffix on the file name unless the -overwrite_original option is used.

- Phil
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 03, 2023, 11:18:30 AM
Quote from: Alastair Gordon on February 03, 2023, 09:03:58 AMThanks, StarGeek, but I still do not know the code I need to simply strip the gps data and then write the modified content, either to the same jpg file, or to another file.

Sorry for not clairfying.  The command you listed would be the correct command for removing the EXIF GPS tags

Example:
C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg
[GPS]          GPSVersionID                    : 2.3.0.0
[GPS]          GPSLatitudeRef                  : North
[GPS]          GPSLatitude                    : 40 deg 41' 21.12"
[GPS]          GPSLongitudeRef                : West
[GPS]          GPSLongitude                    : 74 deg 2' 40.20"
[Composite]    GPSLatitude                    : 40 deg 41' 21.12" N
[Composite]    GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]    GPSPosition                    : 40 deg 41' 21.12" N, 74 deg 2' 40.20" W

C:\>exiftool -P -overwrite_original -gps:all= y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg

C:\>

Phil's example is more complete.  But neither of these will edit data in a XMP sidecar file unless you explicitly name that file, i.e.  use exiftool -location:all= file.NEF will not edit the file.XMP sidecar, which would be the most likely location in the case of RAW file types.

All of these commands create backup files.  Add the -overwrite_original option (https://exiftool.org/exiftool_pod.html#overwrite_original) to suppress this.

If you want to leave the original file alone and create a new copy with the edited information, see the -o (-out) option (https://exiftool.org/exiftool_pod.html#o-OUTFILE-or-FMT--out).
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 03, 2023, 12:22:48 PM
Many thanks, Phil and StarGeek. I certainly appreciate your timely responses. Just to be clear, the ONLY type of image files I am dealing with are .jpg. I guess that means that I am not dealing with a .XMP sidecar file, is that correct?

Also, when I use the following eval in my PHP code for a different part of the website, I capture all the EXIF data from the same .jpg files and everything works perfectly:

eval('$exif=' . `exiftool -php $origFile`);

But in the following code, I am attempting to strip GPS/location data and overwrite the original .jpg file (fear not, I have a backup):

eval(`exiftool -gps:all= -overwrite_original $origJPG`);
eval(`exiftool -location:all= -overwrite_original $origJPG`);

where $origJPG is the full path to the file, starting with https://domain.com/. It also doesn't work if the path is within the same directory (htdocs) as the PHP code with the eval statements.

The problem is that the original file is never overwritten and no new files appear following execution of either (or both) of these eval commands. I know I am doing something wrong, either in the eval statements or in the surrounding PHP script. But I am baffled!

Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 04, 2023, 10:36:20 AM
Quote from: Alastair Gordon on February 03, 2023, 12:22:48 PMwhere $origJPG is the full path to the file, starting with https://domain.com/.

Exiftool can only operate on local file paths.  It cannot read files from an URL.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 04, 2023, 03:22:06 PM
Your right, StarGeek. In fact, I used local paths in my test script with the same result. But here are some interesting new observations.

eval(`exiftool -gps:all= $origFile`); in my test PHP script does, in fact, work and successfully strips the GPS data from the jpg file AND overwrites the original file. However, it throws an error and stops the PHP script at that point. BUT when I run the same script on the same file a 2nd time, all the GPS data is gone. So exiftool did exactly what I asked it to do, but it threw an error and stopped the rest of the PHP script from executing.

The error message was:
Parse error: syntax error, unexpected 'image' (T_STRING) in C:\Apache\htdocs\admin\exifDisplayAnyFile.php(149) : eval()'d code on line 1

By the way when I run the same script on the same file a 2nd time, the same error message is thrown and the PHP script aborts. But it is clear to see that all the GPS data is then gone.

Progress, but still baffled.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 05, 2023, 02:25:11 AM
Quote from: Alastair Gordon on February 04, 2023, 03:22:06 PMHowever, it throws an error and stops the PHP script at that point. BUT when I run the same script on the same file a 2nd time, all the GPS data is gone. So exiftool did exactly what I asked it to do, but it threw an error and stopped the rest of the PHP script from executing.

The error message was:
Parse error: syntax error, unexpected 'image' (T_STRING) in C:\Apache\htdocs\admin\exifDisplayAnyFile.php(149) : eval()'d code on line 1

This isn't an exiftool error response. A quick search seems to indicate there is probably a typo on or around the mentioned line.

Looking deeper, I think the problem is that you're using eval to process the output of exiftool as if it was PHP code.  Your command would cause exiftool to respond with
  1 image files updated
And then eval tries to process that as php.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Phil Harvey on February 05, 2023, 07:35:07 AM
Quote from: StarGeek on February 05, 2023, 02:25:11 AMI think the problem is that you're using eval to process the output of exiftool as if it was PHP code.

I thought that too, but I didn't look deeper.  I should have remembered the exiftool -php option example:

<?phpeval('$array=' . `exiftool -php -q image.jpg`);print_r($array);?>

- Phil
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 05, 2023, 09:04:17 AM
Thanks Phil and StarGeek.

Yes, you're right, even though line 149 is the line throwing the error, eval makes it a line of PHP code, as you say, and that's what PHP is complaining about. In another part of the script where I am just reading the EXIF data, and not trying to change anything, I included the -PHP option, and it has always worked perfectly.

So I will run off and add the -PHP option to eval(`exiftool -gps:all= image.jpg`), and see if that fixes it.

Sorry if a little distracted. Grandbaby number 11 was just born last night. Another name to get wrong!
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Phil Harvey on February 05, 2023, 09:29:44 AM
That won't fix it.  The PHP eval is only good for evaluating the exiftool -php option output.

You just want to use backticks, and no eval at all.

Maybe

$result = `exiftool -gps:all= -overwrite_original $origJPG`;
print_r($result);

But I don't know PHP so I'm just guessing here.

- Phil
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 05, 2023, 11:03:47 AM
Quote from: Phil Harvey on February 05, 2023, 09:29:44 AMYou just want to use backticks, and no eval at all.

That matches the examples I was finding.  I couldn't find any examples that included both backticks and eval. They were always separate and backtick examples were assigned to a variable to capture the output.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 05, 2023, 02:06:30 PM
It works! The trick was adding -php to both reading GPS data (as I already had) and to removing GPS data. Here are the working exiftool statements in PHP:

eval('$exif=' . `exiftool -php $origFile`); 
captures GPS data from JPG file into PHP associative array named $exif.

eval(`exiftool -php -gps:all= $origFile`);
Strips GPS data from JPG file, saves backup of original (adds '_original' to original file name), overwrites original JPG file

Another discovery: exiftool will NOT work with file names that contain a SPACE or '%20' (Hex 20 Ascii for space). Easy to create some PHP to solve that problem.

Again, thanks so much for your help.

Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: StarGeek on February 05, 2023, 03:08:49 PM
Quote from: Alastair Gordon on February 05, 2023, 02:06:30 PMAnother discovery: exiftool will NOT work with file names that contain a SPACE or '%20' (Hex 20 Ascii for space).

The rules for the command line still apply.  If you have a space in the name or some other Windows special character, such as an ampersand, then the argument needs to be quoted or the characters escaped.

Exiftool will only see %20 as exactly what it is, the percent sign followed by two numbers.  As previously stated, exiftool won't read an URL.  Or URI, never can keep them straight.
Title: Re: Cannot strip GPS EXIF data from JPG files
Post by: Alastair Gordon on February 05, 2023, 03:37:07 PM
Putting the originalFile path in quotes (single or double) didn't work either. Also, I did not use the full URL, only the internal path from the test PHP script file to the target JPG file. Only when I created an actual file whose file name contained NO spaces, would exiftool do what I wanted with the JPG file.

So, to summarize, I had 2 problems, which are now fixed and it works perfectly:

1. I had not put the -php switch in the GPS removal command line. Done!

2. Some of the files I was testing had spaces in their file names (Windows 10). Some simple PHP code created an intermediate file with NO spaces in its filename, and presented that space-less filename as $origFile to exifftool.

ExifTool is a fantastic utility, and I even have it working to get location data from MP4 video files.