You are here

function ExifPHPExtension::_reformat in Exif 7

Helper function to reformat fields where required.

Some values (lat/lon) break down into structures, not strings. Dates should be parsed nicely.

1 call to ExifPHPExtension::_reformat()
ExifPHPExtension::readMetadataTags in ./ExifPHPExtension.php
$arOptions liste of options for the method : # enable_sections : (default : TRUE) retrieve also sections.

File

./ExifPHPExtension.php, line 88

Class

ExifPHPExtension

Namespace

Drupal\exif

Code

function _reformat($data) {

  // Make the key lowercase as field names must be.
  $data = array_change_key_case($data, CASE_LOWER);
  foreach ($data as $key => &$value) {
    if (is_array($value)) {
      $value = array_change_key_case($value, CASE_LOWER);
      switch ($key) {

        // GPS values
        case 'gps_latitude':
        case 'gps_longitude':
        case 'gpslatitude':
        case 'gpslongitude':
          $value = $this
            ->_exif_reformat_DMS2D($value, $data[$key . 'ref']);
          break;
      }
    }
    else {
      if (is_string($value)) {
        $value = trim($value);
      }
      if (!drupal_validate_utf8($value)) {
        $value = utf8_encode($value);
      }
      switch ($key) {

        // String values.
        case 'usercomment':
        case 'title':
        case 'comment':
        case 'author':
        case 'subject':
          if ($this
            ->startswith($value, 'UNICODE')) {
            $value = substr($value, 8);
          }
          $value = $this
            ->_exif_reencode_to_utf8($value);
          break;

        // Date values.
        case 'filedatetime':
          $value = date('c', $value);
          break;
        case 'datetimeoriginal':
        case 'datetime':
        case 'datetimedigitized':

          // In case we get a datefield, we need to reformat it
          // to the ISO 8601 standard which will look something
          // like '2004-02-12T15:19:21'.
          $date_time = explode(" ", $value);
          $date_time[0] = str_replace(":", "-", $date_time[0]);
          if (variable_get('exif_granularity', 0) == 1) {
            $date_time[1] = "00:00:00";
          }
          $value = implode("T", $date_time);
          break;

        // GPS values.
        case 'gpsaltitude':
        case 'gpsimgdirection':
          if (!isset($data[$key . 'ref'])) {
            $data[$key . 'ref'] = 0;
          }
          $value = $this
            ->_exif_reformat_DMS2D($value, $data[$key . 'ref']);
          break;
        case 'componentsconfiguration':
        case 'compression':
        case 'contrast':
        case 'exposuremode':
        case 'exposureprogram':
        case 'flash':
        case 'focalplaneresolutionunit':
        case 'gaincontrol':
        case 'lightsource':
        case 'meteringmode':
        case 'orientation':
        case 'resolutionunit':
        case 'saturation':
        case 'scenecapturetype':
        case 'sensingmethod':
        case 'sensitivitytype':
        case 'sharpness':
        case 'subjectdistancerange':
        case 'whitebalance':
          $human_descriptions = $this
            ->getHumanReadableDescriptions()[$key];
          if (isset($human_descriptions[$value])) {
            $value = $human_descriptions[$value];
          }
          break;

        // Exposure values.
        case 'exposuretime':
          if (strpos($value, '/') !== FALSE) {
            $value = $this
              ->_normalise_fraction($value) . 's';
          }
          break;

        // Focal Length values.
        case 'focallength':
          if (strpos($value, '/') !== FALSE) {
            $value = $this
              ->_normalise_fraction($value) . 'mm';
          }
          break;
      }
    }
  }
  return $data;
}