You are here

private function ExifPHPExtension::reformat in Exif 8.2

Same name and namespace in other branches
  1. 8 src/ExifPHPExtension.php \Drupal\exif\ExifPHPExtension::reformat()

Helper function to reformat fields where required.

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

Parameters

array $data: Section data containing all keyword woth associated value.

Return value

array section data containing all keyword woth associated value.

1 call to ExifPHPExtension::reformat()
ExifPHPExtension::readMetadataTags in src/ExifPHPExtension.php
Read all metadata tags.

File

src/ExifPHPExtension.php, line 363

Class

ExifPHPExtension
Class ExifPHPExtension Parser implementation base d on PHP Exif extension.

Namespace

Drupal\exif

Code

private function reformat(array $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
            ->exifReformatGps($value, $data[$key . 'ref']);
          break;
      }
    }
    else {
      if (is_string($value)) {
        $value = trim($value);
      }
      if (!Unicode::validateUtf8($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
            ->reEncodeToUtf8($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]);

          // TODO
          // 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
            ->exifReformatGps($value, $data[$key . 'ref']);
          break;

        // Flash values.
        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
              ->normaliseFraction($value) . 's';
          }
          break;

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