/** * Pull EXIF data from image file * * @param TidypicsImage $image */ function td_get_exif($image) { // catch for those who don't have exif module loaded if (!is_callable('exif_read_data')) { return; } $mime = $image->mimetype; if ($mime != 'image/jpeg' && $mime != 'image/pjpeg') { return; } $filename = $image->getFilenameOnFilestore(); $exif = exif_read_data($filename, "ANY_TAG", true); if (is_array($exif)) { // What data is in the image file? $data = false; // We start with no data if (is_array($exif['IFD0']) && is_array($exif['EXIF'])) { $data = array_merge($exif['IFD0'], $exif['EXIF']); } else { if (is_array($exif['IFD0'])) { $data = $exif['IFD0']; } else { if (is_array($exif['EXIF'])) { $data = $exif['EXIF']; } } } if ($data && is_array($data) && count($data) > 0) { foreach ($data as $key => $value) { if (is_string($value)) { // there are sometimes unicode characters that cause problems with serialize $data[$key] = preg_replace('/[^[:print:]]/', '', $value); } } } if (is_array($exif['GPS'])) { // GPS data $gps_exif = array_intersect_key($exif['GPS'], array_flip(array('GPSLatitudeRef', 'GPSLatitude', 'GPSLongitudeRef', 'GPSLongitude'))); if (count($gps_exif) == 4) { if (is_array($gps_exif['GPSLatitude']) && in_array($gps_exif['GPSLatitudeRef'], array('S', 'N')) && is_array($gps_exif['GPSLongitude']) && in_array($gps_exif['GPSLongitudeRef'], array('W', 'E'))) { $data['latitude'] = parse_exif_gps_data($gps_exif['GPSLatitude'], $gps_exif['GPSLatitudeRef']); $data['longitude'] = parse_exif_gps_data($gps_exif['GPSLongitude'], $gps_exif['GPSLongitudeRef']); } } } if ($data && is_array($data) && count($data) > 0) { $image->tp_exif = serialize($data); } } }
/** * returns informations from EXIF metadata, mapping is done in this function. * * @param string $filename * @param array $map * @return array */ function get_exif_data($filename, $map) { global $conf; $result = array(); if (!function_exists('read_exif_data')) { die('Exif extension not available, admin should disable exif use'); } // Read EXIF data if ($exif = @read_exif_data($filename) or $exif2 = trigger_change('format_exif_data', $exif = null, $filename, $map)) { if (!empty($exif2)) { $exif = $exif2; } else { $exif = trigger_change('format_exif_data', $exif, $filename, $map); } // configured fields foreach ($map as $key => $field) { if (strpos($field, ';') === false) { if (isset($exif[$field])) { $result[$key] = $exif[$field]; } } else { $tokens = explode(';', $field); if (isset($exif[$tokens[0]][$tokens[1]])) { $result[$key] = $exif[$tokens[0]][$tokens[1]]; } } } // GPS data $gps_exif = array_intersect_key($exif, array_flip(array('GPSLatitudeRef', 'GPSLatitude', 'GPSLongitudeRef', 'GPSLongitude'))); if (count($gps_exif) == 4) { if (is_array($gps_exif['GPSLatitude']) and in_array($gps_exif['GPSLatitudeRef'], array('S', 'N')) and is_array($gps_exif['GPSLongitude']) and in_array($gps_exif['GPSLongitudeRef'], array('W', 'E'))) { $result['latitude'] = parse_exif_gps_data($gps_exif['GPSLatitude'], $gps_exif['GPSLatitudeRef']); $result['longitude'] = parse_exif_gps_data($gps_exif['GPSLongitude'], $gps_exif['GPSLongitudeRef']); } } } if (!$conf['allow_html_in_metadata']) { foreach ($result as $key => $value) { // in case the origin of the photo is unsecure (user upload), we remove // HTML tags to avoid XSS (malicious execution of javascript) $result[$key] = strip_tags($value); } } return $result; }