String Search in Shell response

1801 views php
7

I am just a beginner working on small application where user upload images, and it triggers some exiftool which extract and returns metadata from that images. I am interested in extracting only GPS location information from this response i.e. Latitude and Longitude values only. Currently I store the metadata in a variable and echo it out.I am kinda of stuck with that string search in Php or May be directly reading from shell response.

Here is code:

$path = getcwd();    
$metadata = shell_exec("exiftool ".$target_file);
echo $metadata;

Shell Response: I just want to read and return "GPS Longitude : 29 deg 44' 23.10" E" from it.

response

ExifTool Version Number : 11.10 File Name : tree.jpg Directory : uploads File Size : 275 kB File Modification Date/Time : 2018:09:15 14:13:45-07:00 File Access Date/Time : 2018:09:15 14:13:45-07:00 File Creation Date/Time : 2018:09:15 14:13:45-07:00 File Permissions : rw-rw-rw- File Type : JPEG File Type Extension : jpg MIME Type : image/jpeg Exif Byte Order : Little-endian (Intel, II) Make : Nokia Camera Model Name : C5-00.2 Orientation : Horizontal (normal) X Resolution : 300 Y Resolution : 300 Resolution Unit : inches Y Cb Cr Positioning : Centered Warning : Suspicious ExifIFD offset for ExposureTime F Number : 2.4 ISO : 100 Exif Version : . Date/Time Original : Create Date : 8 Components Configuration : Y, Cb, Cr, - Shutter Speed Value : 1/142 Aperture Value : 1.6 Light Source : Unknown Flash : Off, Did not fire Focal Length : 3.4 mm Sub Sec Time : 478 Sub Sec Time Original : 478 Sub Sec Time Digitized : 478 Flashpix Version : . Color Space : sRGB Exif Image Width : 1024 Exif Image Height : 768 Custom Rendered : Normal Exposure Mode : Auto White Balance : Auto Digital Zoom Ratio : 0.005786590576 Scene Capture Type : Standard Gain Control : None GPS Version ID : 8704 GPS Latitude Ref : North GPS Longitude Ref : East GPS Altitude Ref : Above Sea Level Compression : JPEG (old-style) Thumbnail Offset : 13818 Thumbnail Length : 14279 Image Width : 1024 Image Height : 768 Encoding Process : Baseline DCT, Huffman coding Bits Per Sample : 8 Color Components : 3 Y Cb Cr Sub Sampling : YCbCr4:2:0 (2 2) Aperture : 2.4 GPS Altitude : 50.4 m Above Sea Level GPS Longitude : 29 deg 44' 23.10" E Image Size : 1024x768 Megapixels : 0.786 Shutter Speed : 1/142 Thumbnail Image : (Binary data 14279 bytes, use -b option to extract) Focal Length : 3.4 mm Light Value : 9.7

Thanks in Advance.

answered question

Attach var_dump of $metadata variable to the question, please

@MaximFedorov Please find.

You could either read the output per lines and check if line starts with GPS Longitude or then write some regular expression which checks for occurence of GPS Longitude and then reads the rest of line, i.e. reads content until \n character

1 Answer

7

I'm not sure how you want it to work, but if "GPS Longitude" is always in the string you could explode() and iterate through to find the field you are looking for.

$exploded_string = explode(" ", $metadata);
$size_exploded = count(exploded_string); //or you could use sizeof()
$gps_long = "";

for ($i = 0; $i < $size_exploded; $i++){
  if (($exploded_string[$i] == "GPS") && ($exploded_string[$i+1] == "Longitude")){
    $gps_long = array_slice($exploded_string, $i, 8); //this will have the longitude
    break;
  }
}

This is the hard coded way, but if the metadata comes out with each attribute being newline separated then this could be changed. In all honesty, this is a horrible way of doing it, but I don't have much to go off of.

posted this

Have an answer?

JD

Please login first before posting an answer.