Posts in Category: PHP Code Examples

How To: ExifTool php array – example code

php logo
/*==========================================================================
	read exif from exiftool from image into raw array
	two different ways to do it here depending on what you like
============================================================================*/
//exec('exiftool -s ' . '"' . $path_to_image . '"', $exif_raw_array);   
// note the -s flag returns tags with no spaces -n removes data formatting will work with either or none

exec('exiftool -s -n -Orientation -ImageWidth -ImageHeight -Title -ImageDescription -Keywords -Copyright -Artist -Make -Model -LensModel -FocalLength -FocalLengthIn35mmFormat -ShutterSpeedValue -ApertureValue -ISO -DateTimeOriginal -City -State -Country -Location -GPSSpeed -GPSAltitude -GPSLatitude -GPSLongitude ' . '"' . $path_to_image . '"', $exif_raw_array);

/*==========================================================================
	extract tagname and tagname value from raw array
============================================================================*/
// note var_dump(exif_raw_array) to visualize what needs to be done
foreach ($exif_raw_array as $key => $value)  {
	
$string_length_of_value_field = strlen($value); // need string length for calculation below
$position_of_colon = strpos($value, ':'); //get the position of the colon to use in calculation below


$exiftool_tagname = substr($value, 0, $position_of_colon); // shorten the full string starting at position 0 and ending at the colon
$exiftool_tagname = ltrim($exiftool_tagname); // trim any white space on the left that may exist
$exiftool_tagname = rtrim($exiftool_tagname); // trim any white space on the right that may exist
// done - now have $exiftool_tagname extracted by itself


// same technique as above just extracting the other end of the string
$exiftool_tagname_value = substr($value, ($position_of_colon+1), ($string_length_of_value_field-$position_of_colon));
$exiftool_tagname_value = ltrim($exiftool_tagname_value);
$exiftool_tagname_value = rtrim($exiftool_tagname_value);
// done - now have $exiftool_tagname_value extracted by itself


/*==========================================================================
	enter variables into exiftool_final_array with predictable fields
============================================================================*/
$exiftool_final_array[$exiftool_tagname] = $exiftool_tagname_value;


/*==========================================================================
	end of exif_raw_array loop
============================================================================*/
}

/*==========================================================================
	destroy exif_raw_array - no need to keep in memory
============================================================================*/
unset($exif_raw_array);
/*==========================================================================
	done - new clean array is ready to use - try var_dump($exiftool_final_array)
	to see
============================================================================*/