1414use PHPExif \Exif ;
1515use InvalidArgumentException ;
1616use RuntimeException ;
17- use DateTime ;
1817
1918/**
2019 * PHP Exif Exiftool Reader Adapter
@@ -40,6 +39,11 @@ class Exiftool extends AdapterAbstract
4039 */
4140 protected $ numeric = true ;
4241
42+ /**
43+ * @var string
44+ */
45+ protected $ mapperClass = '\\PHPExif \\Mapper \\Exiftool ' ;
46+
4347 /**
4448 * Setter for the exiftool binary path
4549 *
@@ -109,8 +113,16 @@ public function getExifFromFile($file)
109113 );
110114
111115 $ data = json_decode ($ result , true );
112- $ mappedData = $ this ->mapData (reset ($ data ));
113- $ exif = new Exif ($ mappedData );
116+
117+ // map the data:
118+ $ mapper = $ this ->getMapper ();
119+ $ mapper ->setNumeric ($ this ->numeric );
120+ $ mappedData = $ mapper ->mapRawData (reset ($ data ));
121+
122+ // hydrate a new Exif object
123+ $ exif = new Exif ();
124+ $ hydrator = $ this ->getHydrator ();
125+ $ hydrator ->hydrate ($ exif , $ mappedData );
114126 $ exif ->setRawData (reset ($ data ));
115127
116128 return $ exif ;
@@ -148,96 +160,4 @@ protected function getCliOutput($command)
148160
149161 return $ result ;
150162 }
151-
152- /**
153- * Maps native data to Exif format
154- *
155- * @param array $source
156- * @return array
157- */
158- public function mapData (array $ source )
159- {
160- $ focalLength = false ;
161- if (isset ($ source ['FocalLength ' ])) {
162- $ focalLengthParts = explode (' ' , $ source ['FocalLength ' ]);
163- $ focalLength = (int ) reset ($ focalLengthParts );
164- }
165-
166- $ exposureTime = false ;
167- if (isset ($ source ['ExposureTime ' ])) {
168- $ exposureTime = '1/ ' . round (1 / $ source ['ExposureTime ' ]);
169- }
170-
171- $ caption = false ;
172- if (isset ($ source ['Caption ' ])) {
173- $ caption = $ source ['Caption ' ];
174- } elseif (isset ($ source ['Caption-Abstract ' ])) {
175- $ caption = $ source ['Caption-Abstract ' ];
176- }
177-
178- $ gpsLocation = false ;
179- if (isset ($ source ['GPSLatitudeRef ' ]) && isset ($ source ['GPSLongitudeRef ' ])) {
180- $ latitude = $ this ->extractGPSCoordinates ($ source ['GPSLatitude ' ]);
181- $ longitude = $ this ->extractGPSCoordinates ($ source ['GPSLongitude ' ]);
182-
183- if ($ latitude !== false && $ longitude !== false ) {
184- $ gpsLocation = sprintf (
185- '%s,%s ' ,
186- (strtoupper ($ source ['GPSLatitudeRef ' ][0 ]) === 'S ' ? -1 : 1 ) * $ latitude ,
187- (strtoupper ($ source ['GPSLongitudeRef ' ][0 ]) === 'W ' ? -1 : 1 ) * $ longitude
188- );
189- }
190- }
191-
192- return array (
193- Exif::APERTURE => (!isset ($ source ['Aperture ' ])) ?
194- false : sprintf ('f/%01.1f ' , $ source ['Aperture ' ]),
195- Exif::AUTHOR => (!isset ($ source ['Artist ' ])) ? false : $ source ['Artist ' ],
196- Exif::CAMERA => (!isset ($ source ['Model ' ])) ? false : $ source ['Model ' ],
197- Exif::CAPTION => $ caption ,
198- Exif::COLORSPACE => (!isset ($ source [Exif::COLORSPACE ]) ? false : $ source [Exif::COLORSPACE ]),
199- Exif::COPYRIGHT => (!isset ($ source ['Copyright ' ])) ? false : $ source ['Copyright ' ],
200- Exif::CREATION_DATE => (!isset ($ source ['CreateDate ' ])) ?
201- false : DateTime::createFromFormat ('Y:m:d H:i:s ' , $ source ['CreateDate ' ]),
202- Exif::CREDIT => (!isset ($ source ['Credit ' ])) ? false : $ source ['Credit ' ],
203- Exif::EXPOSURE => $ exposureTime ,
204- Exif::FILESIZE => (!isset ($ source [Exif::FILESIZE ]) ? false : $ source [Exif::FILESIZE ]),
205- Exif::FOCAL_LENGTH => $ focalLength ,
206- Exif::FOCAL_DISTANCE => (!isset ($ source ['ApproximateFocusDistance ' ])) ?
207- false : sprintf ('%1$sm ' , $ source ['ApproximateFocusDistance ' ]),
208- Exif::HEADLINE => (!isset ($ source ['Headline ' ])) ? false : $ source ['Headline ' ],
209- Exif::HEIGHT => (!isset ($ source ['ImageHeight ' ])) ? false : $ source ['ImageHeight ' ],
210- Exif::HORIZONTAL_RESOLUTION => (!isset ($ source ['XResolution ' ])) ? false : $ source ['XResolution ' ],
211- Exif::ISO => (!isset ($ source ['ISO ' ])) ? false : $ source ['ISO ' ],
212- Exif::JOB_TITLE => (!isset ($ source ['JobTitle ' ])) ? false : $ source ['JobTitle ' ],
213- Exif::KEYWORDS => (!isset ($ source ['Keywords ' ])) ? false : $ source ['Keywords ' ],
214- Exif::MIMETYPE => (!isset ($ source ['MIMEType ' ])) ? false : $ source ['MIMEType ' ],
215- Exif::ORIENTATION => (!isset ($ source ['Orientation ' ])) ? false : $ source ['Orientation ' ],
216- Exif::SOFTWARE => (!isset ($ source ['Software ' ])) ? false : $ source ['Software ' ],
217- Exif::SOURCE => (!isset ($ source ['Source ' ])) ? false : $ source ['Source ' ],
218- Exif::TITLE => (!isset ($ source ['Title ' ])) ? false : $ source ['Title ' ],
219- Exif::VERTICAL_RESOLUTION => (!isset ($ source ['YResolution ' ])) ? false : $ source ['YResolution ' ],
220- Exif::WIDTH => (!isset ($ source ['ImageWidth ' ])) ? false : $ source ['ImageWidth ' ],
221- Exif::GPS => $ gpsLocation ,
222- );
223- }
224-
225- /**
226- * Extract GPS coordinates from formatted string
227- *
228- * @param string $coordinates
229- * @return array
230- */
231- protected function extractGPSCoordinates ($ coordinates )
232- {
233- if ($ this ->numeric === true ) {
234- return abs ((float ) $ coordinates );
235- } else {
236- if (!preg_match ('!^([0-9.]+) deg ([0-9.]+) \' ([0-9.]+)"! ' , $ coordinates , $ matches )) {
237- return false ;
238- }
239-
240- return intval ($ matches [1 ]) + (intval ($ matches [2 ]) / 60 ) + (floatval ($ matches [3 ]) / 3600 );
241- }
242- }
243163}
0 commit comments