File: Base.php

package info (click to toggle)
php-horde-image 2.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 3,012 kB
  • sloc: php: 8,580; xml: 1,207; sh: 4; makefile: 2
file content (195 lines) | stat: -rw-r--r-- 5,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (LGPL). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 *
 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
 * @category Horde
 * @package  Image
 */

/**
 * Base class for Horde_Image_Exif drivers.
 *
 * @author    Michael J. Rubinsky <mrubinsk@horde.org>
 * @category  Horde
 * @copyright 2009-2017 Horde LLC
 * @package   Image
 */
abstract class Horde_Image_Exif_Base
{
    /**
     * Instance parameters.
     *
     * @var array
     */
    protected $_params;

    /**
     * Optional Logger
     */
    protected $_logger;

    /**
     *
     * @param array $params  Parameter array:
     *        - logger: Horde_Log_Logger  Logger instance.
     */
    public function __construct($params = array())
    {
        if (!empty($params['logger'])) {
            $this->_logger = $params['logger'];
            unset($params['logger']);
        }
        $this->_params = $params;
    }

    /**
     * Process the EXIF data.
     *
     * @param array $exif  Array of EXIF data.
     *
     * @return array  An array of processed EXIF data.
     */
    protected function _processData($exif)
    {
        if (!$exif) {
            return array();
        }

        $results = array();
        $fields = Horde_Image_Exif::getFields($this);
        foreach ($fields as $field => $data) {
            $value = isset($exif[$field]) ? $exif[$field] : '';
            // Don't store empty fields.
            if ($value === '') {
                continue;
            }

            /* Special handling of GPS data */
            if ($data['type'] == 'gps') {
                $value = $this->_parseGPSData($exif[$field]);
                if (!empty($exif[$field . 'Ref']) &&
                    in_array($exif[$field . 'Ref'], array('S', 'South', 'W', 'West'))) {
                    $value = - abs($value);
                }
            }

            /* Date fields are converted to a timestamp.*/
            if ($data['type'] == 'date') {
                @list($ymd, $hms) = explode(' ', $value, 2);
                @list($year, $month, $day) = explode(':', $ymd, 3);
                if (!empty($hms) && !empty($year) && !empty($month) && !empty($day)) {
                    $time = "$month/$day/$year $hms";
                    $value = strtotime($time);
                }
            }

            if ($data['type'] == 'array' || is_array($value)) {
                if (is_array($value)) {
                    $value = implode(',', $value);
                }
            }

            $results[$field] = $value;
        }

        return $results;
    }

    /**
     * Parse the Longitude and Latitude values into a standardized format
     * regardless of the source format.
     *
     * @param mixed $data  An array containing degrees, minutes, seconds
     *                     in index 0, 1, 2 respectifully.
     *
     * @return double  The location data in a decimal format.
     */
    protected function _parseGPSData($data)
    {
        // According to EXIF standard, GPS data can be in the form of
        // dd/1 mm/1 ss/1 or as a decimal reprentation.
        if (!is_array($data)) {
            // Assume a scalar is a decimal representation. Cast it to a float
            // which will get rid of any stray ordinal indicators. (N, S,
            // etc...)
            return (double)$data;
        }

        if ($data[0] == 0) {
            return 0;
        }

        if (strpos($data[0], '/') !== false) {
            $degrees = explode('/', $data[0]);
            if (count($degrees) > 1) {
                $degrees = $degrees[0] / $degrees[1];
            } else {
                $degrees = $degrees[0];
            }
        } else {
            $degrees = $data[0];
        }

        if (strpos($data[1], '/') !== false) {
            $min = explode('/', $data[1]);
            if (count($min) > 1) {
                $min = $min[0] / $min[1];
            } else {
                $min = $min[0];
            }
        } else {
            $min = $data[1];
        }

        if (strpos($data[2], '/') !== false) {
            $sec = explode('/', $data[2]);
            if (count($sec) > 1) {
                $sec = $sec[0] / $sec[1];
            } else {
                $sec = $sec[0];
            }
        } else {
            $sec = $data[2];
        }

        return self::_degToDecimal($degrees, $min, $sec);
    }

    /**
     * Convert degrees representation to decimal representation.
     *
     * @param double $degrees  The degrees latitude or longitude.
     * @param double $minutes  The minutes latitude or longitude.
     * @param double $seconds  the seconds latitude or longitude.
     *
     * @return double  The decimal representation of the latitude or longitute.
     */
    protected function _degToDecimal($degrees, $minutes, $seconds)
    {
        $degs = (double)($degrees + ($minutes / 60) + ($seconds / 3600));

        return round($degs, 6);
    }

    protected function _logDebug($message)
    {
        if (!empty($this->_logger)) {
            $this->_logger->debug($message);
        }
    }

    protected function _logErr($message)
    {
        if (!empty($this->_logger)) {
            $this->_logger->err($message);
        }
    }

    abstract public function getData($image);

    abstract public function supportedCategories();
}