function write_metadata($path, $ref, $uniqid="") { // copys the file to tmp and runs exiftool on it // uniqid tells the tmp file to be placed in an isolated folder within tmp global $exiftool_remove_existing,$storagedir,$exiftool_write,$exiftool_no_process,$mysql_charset; # Fetch file extension and resource type. $resource_data=get_resource_data($ref); $extension=$resource_data["file_extension"]; $resource_type=$resource_data["resource_type"]; $exiftool_fullpath = get_utility_path("exiftool"); # Check if an attempt to write the metadata shall be performed. if (($exiftool_fullpath!=false) && ($exiftool_write) && !in_array($extension,$exiftool_no_process)) { $filename = pathinfo($path); $filename = $filename['basename']; $tmpfile=get_temp_dir(false,$uniqid) . "/" . $filename; copy($path,$tmpfile); # Add the call to exiftool and some generic arguments to the command string. # Argument -overwrite_original: Now that we have already copied the original file, we can use exiftool's overwrite_original on the tmpfile. # Argument -E: Escape values for HTML. Used for handling foreign characters in shells not using UTF-8. # Arguments -EXIF:all= -XMP:all= -IPTC:all=: Remove the metadata in the tag groups EXIF, XMP and IPTC. $command = $exiftool_fullpath . " -overwrite_original -E "; if ($exiftool_remove_existing) {$command.= "-EXIF:all= -XMP:all= -IPTC:all= ";} $write_to = get_exiftool_fields($resource_type); # Returns an array of exiftool fields for the particular resource type, which are basically fields with an 'exiftool field' set. for($i = 0; $i<count($write_to); $i++) # Loop through all the found fields. { $fieldtype = $write_to[$i]['type']; $writevalue = ""; # Formatting and cleaning of the value to be written - depending on the RS field type. switch ($fieldtype) { case 2: # Check box list: remove initial comma if present if (substr(get_data_by_field($ref, $write_to[$i]['ref']), 0, 1)==",") {$writevalue = substr(get_data_by_field($ref, $write_to[$i]['ref']), 1);} else {$writevalue = get_data_by_field($ref, $write_to[$i]['ref']);} break; case 3: # Drop down list: remove initial comma if present if (substr(get_data_by_field($ref, $write_to[$i]['ref']), 0, 1)==",") {$writevalue = substr(get_data_by_field($ref, $write_to[$i]['ref']), 1);} else {$writevalue = get_data_by_field($ref, $write_to[$i]['ref']);} break; case 4: # Date: write datetype fields as ISO 8601 date ("c") $datecheck=get_data_by_field($ref, $write_to[$i]['ref']); if ($datecheck!=""){ $writevalue = date("c", strtotime($datecheck)); } break; case 6: # Expiry Date: write datetype fields as ISO 8601 date ("c") $datecheck=get_data_by_field($ref, $write_to[$i]['ref']); if ($datecheck!=""){ $writevalue = date("c", strtotime($datecheck)); } break; case 9: # Dynamic Keywords List: remove initial comma if present if (substr(get_data_by_field($ref, $write_to[$i]['ref']), 0, 1)==",") {$writevalue = substr(get_data_by_field($ref, $write_to[$i]['ref']), 1);} else {$writevalue = get_data_by_field($ref, $write_to[$i]['ref']);} break; default: # Other types $writevalue = get_data_by_field($ref, $write_to[$i]['ref']); } # Add the tag name(s) and the value to the command string. $group_tags = explode(",", $write_to[$i]['exiftool_field']); # Each 'exiftool field' may contain more than one tag. foreach ($group_tags as $group_tag) { $group_tag = strtolower($group_tag); # E.g. IPTC:Keywords -> iptc:keywords if (strpos($group_tag,":")===false) {$tag = $group_tag;} # E.g. subject -> subject else {$tag = substr($group_tag, strpos($group_tag,":")+1);} # E.g. iptc:keywords -> keywords switch ($tag) { case "filesize": # Do nothing, no point to try to write the filesize. break; case "keywords": # Keywords shall be written one at a time and not all together. $keywords = explode(",", $writevalue); # "keyword1,keyword2, keyword3" (with or with spaces) foreach ($keywords as $keyword) { # Trim leading space if any. if (substr($keyword, 0, 1)==" ") {$keyword = substr($keyword, 1);} # Convert the data to UTF-8 if not already. if (!isset($mysql_charset) || (isset($mysql_charset) && strtolower($mysql_charset)!="utf8")){$keyword = mb_convert_encoding($keyword, 'UTF-8');} $command.= escapeshellarg("-" . $group_tag . "=" . htmlentities($keyword, ENT_QUOTES, "UTF-8")) . " "; } break; default: # Convert the data to UTF-8 if not already. if (!isset($mysql_charset) || (isset($mysql_charset) && strtolower($mysql_charset)!="utf8")){$writevalue = mb_convert_encoding($writevalue, 'UTF-8');} $command.= escapeshellarg("-" . $group_tag . "=" . htmlentities($writevalue, ENT_QUOTES, "UTF-8")) . " "; } } } # Add the filename to the command string. $command.= " " . escapeshellarg($tmpfile); # Perform the actual writing - execute the command string. $output = run_command($command); return $tmpfile; } else { return false; } }
$results_simulated[$i]["tag"] = trim(strtolower($tag_value[1])); $results_simulated[$i]["value"] = trim($tag_value[2]); $tagprops = ""; if (in_array($results_simulated[$i]["tag"], $writable_tags_array) && $file_writability) { $tagprops .= "w"; } if ($tagprops != "") { $results_simulated[$i]["tagprops"] = "({$tagprops})"; } else { $results_simulated[$i]["tagprops"] = ""; } $i++; } } # Create a list of resource fields which are mapped to exiftool tags. $write_to = get_exiftool_fields($resource_type); # Returns an array of exiftool tags for the particular resource type, which are basically RS resource fields with an 'exiftool field' set. for ($i = 0; $i < count($write_to); $i++) { # Populate the resourcefields array. $tags = explode(",", $write_to[$i]['exiftool_field']); # Each 'exiftool field' may contain more than one tag. foreach ($tags as $tag) { $tag = strtolower($tag); $resourcefields[$tag]['ref'] = $write_to[$i]['ref']; $resourcefields[$tag]['listed'] = false; } } # Build report: # Work out the write status. if (!isset($file_writability)) { $file_writability = true;
function extract_exif_comment($ref, $extension = "") { # Extract the EXIF comment from either the ImageDescription field or the UserComment # Also parse IPTC headers and insert # EXIF headers $exifoption = getval("no_exif", ""); // This may have been set to a non-standard value if allowing per field selection if ($exifoption == "yes") { $exifoption = "no"; } // Sounds odd but previously was no_exif so logic reversed if ($exifoption == "") { $exifoption = "yes"; } $image = get_resource_path($ref, true, "", false, $extension); if (!file_exists($image)) { return false; } hook("pdfsearch"); global $exif_comment, $exiftool_no_process, $exiftool_resolution_calc, $disable_geocoding, $embedded_data_user_select_fields, $filename_field; $exiftool_fullpath = get_utility_path("exiftool"); if ($exiftool_fullpath != false && !in_array($extension, $exiftool_no_process)) { $resource = get_resource_data($ref); # Field 8 is used in a special way for staticsync; don't overwrite. if ($resource['file_path'] != "") { $omit_title_for_staticsync = true; } else { $omit_title_for_staticsync = false; } hook("beforeexiftoolextraction"); if ($exiftool_resolution_calc) { # see if we can use exiftool to get resolution/units, and dimensions here. # Dimensions are normally extracted once from the view page, but for the original file, it should be done here if possible, # and exiftool can provide more data. $command = $exiftool_fullpath . " -s -s -s -t -composite:imagesize -xresolution -resolutionunit " . escapeshellarg($image); $dimensions_resolution_unit = explode("\t", run_command($command)); # if dimensions resolution and unit could be extracted, add them to the database. # they can be used in view.php to give more accurate data. if (count($dimensions_resolution_unit) == 3) { $dru = $dimensions_resolution_unit; $filesize = filesize_unlimited($image); $wh = explode("x", $dru[0]); $width = $wh[0]; $height = $wh[1]; $resolution = $dru[1]; $unit = $dru[2]; sql_query("insert into resource_dimensions (resource, width, height, resolution, unit, file_size) values ('{$ref}', '{$width}', '{$height}', '{$resolution}', '{$unit}', '{$filesize}')"); } } $read_from = get_exiftool_fields($resource['resource_type']); # run exiftool to get all the valid fields. Use -s -s option so that # the command result isn't printed in columns, which will help in parsing # We then split the lines in the result into an array $command = $exiftool_fullpath . " -s -s -f -m -d \"%Y-%m-%d %H:%M:%S\" -G " . escapeshellarg($image); $metalines = explode("\n", run_command($command)); $metadata = array(); # an associative array to hold metadata field/value pairs # go through each line and split field/value using the first # occurrance of ": ". The keys in the associative array is converted # into uppercase for easier lookup later foreach ($metalines as $metaline) { # Use stripos() if available, but support earlier PHP versions if not. if (function_exists("stripos")) { $pos = stripos($metaline, ": "); } else { $pos = strpos($metaline, ": "); } if ($pos) { # add to the associative array, also clean up leading/trailing space & single quote (on windows sometimes) # Extract group name and tag name. $s = explode("]", substr($metaline, 0, $pos)); if (count($s) > 1 && strlen($s[0]) > 1) { # Extract value $value = trim(substr($metaline, $pos + 2)); # Replace '..' with line feed - either Exiftool itself or Adobe Bridge replaces line feeds with '..' $value = str_replace('....', '\\n\\n', $value); // Two new line feeds in ExifPro are replaced with 4 dots '....' $value = str_replace('...', '.\\n', $value); # Three dots together is interpreted as a full stop then line feed, not the other way round $value = str_replace('..', '\\n', $value); # Extract group name and tag name $groupname = strtoupper(substr($s[0], 1)); $tagname = strtoupper(trim($s[1])); # Store both tag data under both tagname and groupname:tagname, to support both formats when mapping fields. $metadata[$tagname] = $value; $metadata[$groupname . ":" . $tagname] = $value; debug("Exiftool: extracted field '{$groupname}:{$tagname}', value is '{$value}'"); } } } // We try to fetch the original filename from database. $resources = sql_query("SELECT resource.file_path FROM resource WHERE resource.ref = " . $ref); if ($resources) { $resource = $resources[0]; if ($resource['file_path']) { $metadata['FILENAME'] = mb_basename($resource['file_path']); } } if (isset($metadata['FILENAME'])) { $metadata['STRIPPEDFILENAME'] = strip_extension($metadata['FILENAME']); } # Geolocation Metadata Support if (!$disable_geocoding && isset($metadata['GPSLATITUDE'])) { # Set vars $dec_long = 0; $dec_lat = 0; #Convert latititude to decimal. if (preg_match("/^(?<degrees>\\d+) deg (?<minutes>\\d+)' (?<seconds>\\d+\\.?\\d*)\"/", $metadata['GPSLATITUDE'], $latitude)) { $dec_lat = $latitude['degrees'] + $latitude['minutes'] / 60 + $latitude['seconds'] / (60 * 60); } if (preg_match("/^(?<degrees>\\d+) deg (?<minutes>\\d+)' (?<seconds>\\d+\\.?\\d*)\"/", $metadata['GPSLONGITUDE'], $longitude)) { $dec_long = $longitude['degrees'] + $longitude['minutes'] / 60 + $longitude['seconds'] / (60 * 60); } if (strpos($metadata['GPSLATITUDE'], 'S') !== false) { $dec_lat = -1 * $dec_lat; } if (strpos($metadata['GPSLONGITUDE'], 'W') !== false) { $dec_long = -1 * $dec_long; } if ($dec_long != 0 && $dec_lat != 0) { sql_query("update resource set geo_long='" . escape_check($dec_long) . "',geo_lat='" . escape_check($dec_lat) . "' where ref='{$ref}'"); } } # Update portrait_landscape_field (when reverting metadata this was getting lost) update_portrait_landscape_field($ref); # now we lookup fields from the database to see if a corresponding value # exists in the uploaded file $exif_updated_fields = array(); for ($i = 0; $i < count($read_from); $i++) { $field = explode(",", $read_from[$i]['exiftool_field']); foreach ($field as $subfield) { $subfield = strtoupper($subfield); // convert to upper case for easier comparision if (in_array($subfield, array_keys($metadata)) && $metadata[$subfield] != "-" && trim($metadata[$subfield]) != "") { $read = true; $value = $metadata[$subfield]; # Dropdown box or checkbox list? if ($read_from[$i]["type"] == 2 || $read_from[$i]["type"] == 3) { # Check that the value is one of the options and only insert if it is an exact match. # The use of safe_file_name and strtolower ensures matching takes place on alphanumeric characters only and ignores case. # First fetch all options in all languages $options = trim_array(explode(",", strtolower($read_from[$i]["options"]))); for ($n = 0; $n < count($options); $n++) { $options[$n] = $options[$n]; } # If not in the options list, do not read this value $s = trim_array(explode(",", $value)); $value = ""; # blank value for ($n = 0; $n < count($s); $n++) { if (trim($s[0]) != "" && in_array(strtolower($s[$n]), $options)) { $value .= "," . $s[$n]; } } #echo($read_from[$i]["ref"] . " = " . $value . "<br>"); } # Read the data. if ($read) { $plugin = dirname(__FILE__) . "/../plugins/exiftool_filter_" . $read_from[$i]['name'] . ".php"; if ($read_from[$i]['exiftool_filter'] != "") { eval($read_from[$i]['exiftool_filter']); } if (file_exists($plugin)) { include $plugin; } # Field 8 is used in a special way for staticsync; don't overwrite field 8 in this case if (!($omit_title_for_staticsync && $read_from[$i]['ref'] == 8)) { $exiffieldoption = $exifoption; if ($exifoption == "custom" || isset($embedded_data_user_select_fields) && in_array($read_from[$i]['ref'], $embedded_data_user_select_fields)) { debug("EXIF - custom option for field " . $read_from[$i]['ref'] . " : " . $exifoption); $exiffieldoption = getval("exif_option_" . $read_from[$i]['ref'], $exifoption); } debug("EXIF - option for field " . $read_from[$i]['ref'] . " : " . $exiffieldoption); if ($exiffieldoption == "no") { continue; } elseif ($exiffieldoption == "append") { $spacechar = $read_from[$i]["type"] == 2 || $read_from[$i]["type"] == 3 ? ", " : " "; $oldval = get_data_by_field($ref, $read_from[$i]['ref']); if (strpos($oldval, $value) !== false) { continue; } $newval = $oldval . $spacechar . iptc_return_utf8($value); } elseif ($exiffieldoption == "prepend") { $spacechar = $read_from[$i]["type"] == 2 || $read_from[$i]["type"] == 3 ? ", " : " "; $oldval = get_data_by_field($ref, $read_from[$i]['ref']); if (strpos($oldval, $value) !== false) { continue; } $newval = iptc_return_utf8($value) . $spacechar . $oldval; } else { $newval = iptc_return_utf8($value); } global $merge_filename_with_title, $lang; if ($merge_filename_with_title) { $merge_filename_with_title_option = urlencode(getval('merge_filename_with_title_option', '')); $merge_filename_with_title_include_extensions = urlencode(getval('merge_filename_with_title_include_extensions', '')); $merge_filename_with_title_spacer = urlencode(getval('merge_filename_with_title_spacer', '')); $original_filename = ''; if (isset($_REQUEST['name'])) { $original_filename = $_REQUEST['name']; } else { $original_filename = $processfile['name']; } if ($merge_filename_with_title_include_extensions == 'yes') { $merged_filename = $original_filename; } else { $merged_filename = strip_extension($original_filename); } $oldval = get_data_by_field($ref, $read_from[$i]['ref']); if (strpos($oldval, $value) !== FALSE) { continue; } switch ($merge_filename_with_title_option) { case $lang['merge_filename_title_do_not_use']: // Do nothing since the user doesn't want to use this feature break; case $lang['merge_filename_title_replace']: $newval = $merged_filename; break; case $lang['merge_filename_title_prefix']: $newval = $merged_filename . $merge_filename_with_title_spacer . $oldval; if ($oldval == '') { $newval = $merged_filename; } break; case $lang['merge_filename_title_suffix']: $newval = $oldval . $merge_filename_with_title_spacer . $merged_filename; if ($oldval == '') { $newval = $merged_filename; } break; default: // Do nothing break; } } update_field($ref, $read_from[$i]['ref'], $newval); $exif_updated_fields[] = $read_from[$i]['ref']; hook("metadata_extract_addition", "all", array($ref, $newval, $read_from, $i)); } } } else { // Process if no embedded title is found: global $merge_filename_with_title, $lang; if ($merge_filename_with_title && $read_from[$i]['ref'] == 8) { $merge_filename_with_title_option = urlencode(getval('merge_filename_with_title_option', '')); $merge_filename_with_title_include_extensions = urlencode(getval('merge_filename_with_title_include_extensions', '')); $merge_filename_with_title_spacer = urlencode(getval('merge_filename_with_title_spacer', '')); $original_filename = ''; if (isset($_REQUEST['name'])) { $original_filename = $_REQUEST['name']; } else { $original_filename = $processfile['name']; } if ($merge_filename_with_title_include_extensions == 'yes') { $merged_filename = $original_filename; } else { $merged_filename = strip_extension($original_filename); } $oldval = get_data_by_field($ref, $read_from[$i]['ref']); if (strpos($oldval, $value) !== FALSE) { continue; } switch ($merge_filename_with_title_option) { case $lang['merge_filename_title_do_not_use']: // Do nothing since the user doesn't want to use this feature break; case $lang['merge_filename_title_replace']: $newval = $merged_filename; break; case $lang['merge_filename_title_prefix']: $newval = $merged_filename . $merge_filename_with_title_spacer . $oldval; if ($oldval == '') { $newval = $merged_filename; } break; case $lang['merge_filename_title_suffix']: $newval = $oldval . $merge_filename_with_title_spacer . $merged_filename; if ($oldval == '') { $newval = $merged_filename; } break; default: // Do nothing break; } update_field($ref, $read_from[$i]['ref'], $newval); $exif_updated_fields[] = $read_from[$i]['ref']; } } } } if (!in_array($filename_field, $exif_updated_fields)) { $exiffilenameoption = getval("exif_option_" . $filename_field, $exifoption); debug("EXIF - custom option for filename field " . $filename_field . " : " . $exiffilenameoption); if ($exiffilenameoption != "yes") { $uploadedfilename = isset($_REQUEST['name']) ? $_REQUEST['name'] : $processfile['name']; global $userref, $amended_filename; $entered_filename = get_data_by_field(-$userref, $filename_field); debug("EXIF - got entered file name " . $entered_filename); if ($exiffilenameoption == "no") { $amended_filename = $entered_filename; if (trim($amended_filename) == '') { $amended_filename = $uploadedfilename; } if (strpos($amended_filename, $extension) === FALSE) { $amended_filename .= '.' . $extension; } } elseif ($exiffilenameoption == "append") { $amended_filename = $entered_filename . $uploadedfilename; } elseif ($exiffilenameoption == "prepend") { $amended_filename = strip_extension($uploadedfilename) . $entered_filename . "." . $extension; } debug("EXIF - created new file name " . $amended_filename); } } } elseif (isset($exif_comment)) { # # Exiftool is not installed. As a fallback we grab some predefined basic fields using the PHP function # exif_read_data() # if (function_exists("exif_read_data")) { $data = @exif_read_data($image); } else { $data = false; } if ($data !== false) { $comment = ""; #echo "<pre>EXIF\n";print_r($data);exit(); if (isset($data["ImageDescription"])) { $comment = $data["ImageDescription"]; } if ($comment == "" && isset($data["COMPUTED"]["UserComment"])) { $comment = $data["COMPUTED"]["UserComment"]; } if ($comment != "") { # Convert to UTF-8 $comment = iptc_return_utf8($comment); # Save comment global $exif_comment; update_field($ref, $exif_comment, $comment); } if (isset($data["Model"])) { # Save camera make/model global $exif_model; update_field($ref, $exif_model, $data["Model"]); } if (isset($data["DateTimeOriginal"])) { # Save camera date/time global $exif_date; $date = $data["DateTimeOriginal"]; # Reformat date to ISO standard $date = substr($date, 0, 4) . "-" . substr($date, 5, 2) . "-" . substr($date, 8, 11); update_field($ref, $exif_date, $date); } } # Try IPTC headers $size = getimagesize($image, $info); if (isset($info["APP13"])) { $iptc = iptcparse($info["APP13"]); #echo "<pre>IPTC\n";print_r($iptc);exit(); # Look for iptc fields, and insert. $fields = sql_query("select * from resource_type_field where length(iptc_equiv)>0"); for ($n = 0; $n < count($fields); $n++) { $iptc_equiv = $fields[$n]["iptc_equiv"]; if (isset($iptc[$iptc_equiv][0])) { # Found the field if (count($iptc[$iptc_equiv]) > 1) { # Multiple values (keywords) $value = ""; for ($m = 0; $m < count($iptc[$iptc_equiv]); $m++) { if ($m > 0) { $value .= ", "; } $value .= $iptc[$iptc_equiv][$m]; } } else { $value = $iptc[$iptc_equiv][0]; } $value = iptc_return_utf8($value); # Date parsing if ($fields[$n]["type"] == 4) { $value = substr($value, 0, 4) . "-" . substr($value, 4, 2) . "-" . substr($value, 6, 2); } if (trim($value) != "") { update_field($ref, $fields[$n]["ref"], $value); } } } } } # Update the XML metadata dump file. update_xml_metadump($ref); # Auto fill any blank fields. autocomplete_blank_fields($ref); }
function write_metadata($path, $ref, $uniqid = "") { // copys the file to tmp and runs exiftool on it // uniqid tells the tmp file to be placed in an isolated folder within tmp global $exiftool_remove_existing, $storagedir, $exiftool_write, $exiftool_no_process; # Fetch file extension $resource_data = get_resource_data($ref); $extension = $resource_data["file_extension"]; $resource_type = $resource_data["resource_type"]; $exiftool_fullpath = get_utility_path("exiftool"); if ($exiftool_fullpath != false && $exiftool_write && !in_array($extension, $exiftool_no_process)) { $filename = pathinfo($path); $filename = $filename['basename']; $tmpfile = get_temp_dir(false, $uniqid) . "/" . $filename; copy($path, $tmpfile); #Now that we have already copied the original file, we can use exiftool's overwrite_original on the tmpfile. # Argument -E: escape values for HTML. Used for handling foreign characters in shells not using UTF-8. $command = $exiftool_fullpath . " -overwrite_original -E "; if ($exiftool_remove_existing) { $command .= "-EXIF:all -XMP:all= -IPTC:all= "; } $write_to = get_exiftool_fields($resource_type); for ($i = 0; $i < count($write_to); $i++) { $fieldtype = $write_to[$i]['type']; $field = explode(",", $write_to[$i]['exiftool_field']); # write datetype fields as ISO 8601 date ("c") if ($fieldtype == "4") { $writevalue = date("c", strtotime(get_data_by_field($ref, $write_to[$i]['ref']))); } else { $writevalue = get_data_by_field($ref, $write_to[$i]['ref']); } # Remove initial comma (for checkbox lists) if (substr($writevalue, 0, 1) == ",") { $writevalue = substr($writevalue, 1); } foreach ($field as $field) { $command .= escapeshellarg("-" . $field . "=" . htmlentities($writevalue, ENT_QUOTES, "UTF-8")) . " "; } } $command .= " " . escapeshellarg($tmpfile); $output = run_command($command); return $tmpfile; } else { return false; } }