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:

1 call to ExifPHPExtension::_reformat()
ExifPHPExtension::readMetadataTags in ./ExifPHPExtension.php
List of options for the method.

File

./ExifPHPExtension.php, line 87

Class

ExifPHPExtension
This is a helper class to handle the whole data processing of exif.

Namespace

Drupal\exif

Code

public 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->_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 (strstartswith($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;
}