forked from cnlpete/image-metadata-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageMetadataParser.php
217 lines (181 loc) · 6.67 KB
/
imageMetadataParser.php
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
* This file is a part of the Image Metadata Parser Library.
*
* (c) 2013 Hauke Schade.
*
* For the full copyright and license information, please view the license.txt
* file that was distributed with this source code.
*/
class ImageMetadataParser {
protected $sFilename;
protected $aAttributes = array();
public function __construct($sFilename) {
$this->sFilename = $sFilename;
}
public static function exifAvailable() {
$load_ext = get_loaded_extensions();
return in_array('exif', $load_ext);
}
public function parseExif() {
$aArr = @exif_read_data($this->sFilename, 'IFD0,THUMBNAIL', true);
if ($aArr === false)
return false;
// the date and time the image was taken
if (isset($aArr['IFD0']['DateTime'])) {
$iTimestamp = self::timestampFromEXIF($aArr['IFD0']['DateTime']);
if ($iTimestamp !== false)
$this->aAttributes['datetime'] = $iTimestamp;
}
else if (isset($aArr['EXIF']['DateTimeOriginal'])) {
$iTimestamp = self::timestampFromEXIF($aArr['EXIF']['DateTimeOriginal']);
if ($iTimestamp !== false)
$this->aAttributes['datetime'] = $iTimestamp;
}
else if (isset($aArr['EXIF']['DateTimeDigitized'])) {
$iTimestamp = self::timestampFromEXIF($aArr['EXIF']['DateTimeDigitized']);
if ($iTimestamp !== false)
$this->aAttributes['datetime'] = $iTimestamp;
}
// the images title
if (isset($aArr['COMPUTED']['UserComment']))
$this->aAttributes['title'] = trim($aArr['COMPUTED']['UserComment']);
// the thumbnails mimetype
if (isset($aArr['COMPUTED']['Thumbnail.MimeType']))
$this->aAttributes['thumbnailtype'] = $aArr['COMPUTED']['Thumbnail.MimeType'];
// gps
if (isset($aArr['GPS']))
$this->aAttributes['gps'] = $aArr['GPS'];
// the exif orientation
if (isset($aArr['IFD0']['Orientation']))
$this->aAttributes['orientation'] = $aArr['IFD0']['Orientation'];
return true;
}
public function parseIPTC() {
$aArr = @exif_read_data($this->sFilename, 'IDF0', true);
$size = getimagesize($this->sFilename, $info);
if(!isset($info['APP13']))
return false;
$iptc = iptcparse($info['APP13']);
if (isset($iptc["2#120"][0])) # caption
$this->aAttributes['title'] = trim($iptc["2#120"][0]);
else if (isset($iptc["2#105"][0])) # headline
$this->aAttributes['title'] = trim($iptc["2#105"][0]);
else if (isset($iptc["2#005"][0])) # graphic name
$this->aAttributes['title'] = trim($iptc["2#005"][0]);
if (isset($iptc["2#055"][0]) && isset($iptc["2#060"][0])) {# creation date
$iTimestamp = self::timestampFromIPTC($iptc["2#055"][0], $iptc["2#060"][0]);
if ($iTimestamp !== false)
$this->aAttributes['datetime'] = $iTimestamp;
}
return true;
}
private function timestampFromIPTC( $date, $time ) {
if ( ! ( preg_match('/\d\d\d\d\d\d[-+]\d\d\d\d/', $time)
&& preg_match('/\d\d\d\d\d\d\d\d/', $date)
&& substr($date, 0, 8) !== '00000000' ) ) {
// wrong dates
return false;
}
$iTimestamp = mktime(
substr( $time, 0, 2 ),
substr( $time, 2, 2 ),
substr( $time, 4, 2 ),
substr( $date, 4, 2 ),
substr( $date, 6, 2 ),
substr( $date, 0, 4 ));
$iDiff = ( intval( substr( $time, 7, 2 ) ) *60*60 )
+ ( intval( substr( $time, 9, 2 ) ) * 60 );
if ( substr( $time, 6, 1 ) === '-' )
$iDiff = - $iDiff;
return $iTimestamp + $iDiff;
}
private function timestampFromEXIF( $string ) {
if ( ! ( preg_match('/\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/', $string))) {
// wrong date
return false;
}
$iTimestamp = mktime(
substr( $string, 11, 2 ),
substr( $string, 14, 2 ),
substr( $string, 17, 2 ),
substr( $string, 5, 2 ),
substr( $string, 8, 2 ),
substr( $string, 0, 4 ));
return $iTimestamp;
}
public function hasTitle() {
return isset($this->aAttributes['title']);
}
public function getTitle() {
return (string)$this->aAttributes['title'];
}
public function hasThumbnail() {
return (isset($this->aAttributes['thumbnailtype']));
}
public function getThumbnail() {
return exif_thumbnail($this->sFilename);
}
public function getThumbnailContentType() {
return $this->aAttributes['thumbnailtype'];
}
public function hasDateTime() {
return isset($this->aAttributes['datetime']);
}
public function getDateTime() {
return (int)$this->aAttributes['datetime'];
}
public function hasGPS() {
return isset($this->aAttributes['gps']) &&
isset($this->aAttributes['gps']['GPSLongitude'][0]) &&
isset($this->aAttributes['gps']['GPSLatitude'][0]);
}
public function getGPS(&$dLat, &$dLong) {
$latFirst = explode("/", $this->aAttributes['gps']['GPSLatitude'][0]);
$latSecond = explode("/", $this->aAttributes['gps']['GPSLatitude'][1]);
$latThird = explode("/", $this->aAttributes['gps']['GPSLatitude'][2]);
$latRef = isset($this->aAttributes['gps']['GPSLatitudeRef']) ? $this->aAttributes['gps']['GPSLatitudeRef'] : 'N';
$latFirst = intval($latFirst[0]) / intval($latFirst[1]);
$latSecond = intval($latSecond[0])/ intval($latSecond[1]);
$latThird = intval($latThird[0]) / intval($latThird[1]);
$dLat = $latFirst + ($latSecond*60 + $latThird) / 3600;
if ($latRef == 'S')
$dLat *= -1;
$longFirst = explode("/", $this->aAttributes['gps']['GPSLongitude'][0]);
$longSecond = explode("/", $this->aAttributes['gps']['GPSLongitude'][1]);
$longThird = explode("/", $this->aAttributes['gps']['GPSLongitude'][2]);
$longRef = isset($this->aAttributes['gps']['GPSLongitudeRef']) ? $this->aAttributes['gps']['GPSLongitudeRef'] : 'E';
$longFirst = intval($longFirst[0]) / intval($longFirst[1]);
$longSecond = intval($longSecond[0])/ intval($longSecond[1]);
$longThird = intval($longThird[0]) / intval($longThird[1]);
$dLong = $longFirst + ($longSecond*60 + $longThird) / 3600;
if ($longRef == 'W')
$dLat *= -1;
}
public function getGPSArray() {
$dLat = 0.0; $dLong = 0.0;
$this->getGPS($dLat, $dLong);
return array('lat' => $dLat, 'long' => $dLong);
}
public function hasOrientation() {
return isset($this->aAttributes['orientation']);
}
public function getOrientation() {
switch($this->aAttributes['orientation']) {
case 3:
return 180;
break;
case 6:
return -90;
break;
case 8:
return 90;
break;
default:
return 0;
}
}
public function getRawOrientation() {
return $this->aAttributes['orientation'];
}
}