Exiftool.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * PHP Exif Exiftool Mapper
  4. *
  5. * @link http://github.com/miljar/PHPExif for the canonical source repository
  6. * @copyright Copyright (c) 2015 Tom Van Herreweghe <tom@theanalogguy.be>
  7. * @license http://github.com/miljar/PHPExif/blob/master/LICENSE MIT License
  8. * @category PHPExif
  9. * @package Mapper
  10. */
  11. namespace PHPExif\Mapper;
  12. use PHPExif\Exif;
  13. use DateTime;
  14. /**
  15. * PHP Exif Exiftool Mapper
  16. *
  17. * Maps Exiftool raw data to valid data for the \PHPExif\Exif class
  18. *
  19. * @category PHPExif
  20. * @package Mapper
  21. */
  22. class Exiftool implements MapperInterface
  23. {
  24. const APERTURE = 'Composite:Aperture';
  25. const APPROXIMATEFOCUSDISTANCE = 'XMP-aux:ApproximateFocusDistance';
  26. const ARTIST = 'IFD0:Artist';
  27. const CAPTION = 'XMP-acdsee';
  28. const CAPTIONABSTRACT = 'IPTC:Caption-Abstract';
  29. const COLORSPACE = 'ExifIFD:ColorSpace';
  30. const COPYRIGHT = 'IFD0:Copyright';
  31. const DATETIMEORIGINAL = 'ExifIFD:DateTimeOriginal';
  32. const CREDIT = 'IPTC:Credit';
  33. const EXPOSURETIME = 'ExifIFD:ExposureTime';
  34. const FILESIZE = 'System:FileSize';
  35. const FOCALLENGTH = 'ExifIFD:FocalLength';
  36. const HEADLINE = 'IPTC:Headline';
  37. const IMAGEHEIGHT = 'File:ImageHeight';
  38. const IMAGEWIDTH = 'File:ImageWidth';
  39. const ISO = 'ExifIFD:ISO';
  40. const JOBTITLE = 'IPTC:By-lineTitle';
  41. const KEYWORDS = 'IPTC:Keywords';
  42. const MIMETYPE = 'File:MIMEType';
  43. const MODEL = 'IFD0:Model';
  44. const ORIENTATION = 'IFD0:Orientation';
  45. const SOFTWARE = 'IFD0:Software';
  46. const SOURCE = 'IPTC:Source';
  47. const TITLE = 'IPTC:ObjectName';
  48. const XRESOLUTION = 'IFD0:XResolution';
  49. const YRESOLUTION = 'IFD0:YResolution';
  50. const GPSLATITUDE = 'GPS:GPSLatitude';
  51. const GPSLONGITUDE = 'GPS:GPSLongitude';
  52. /**
  53. * Maps the ExifTool fields to the fields of
  54. * the \PHPExif\Exif class
  55. *
  56. * @var array
  57. */
  58. protected $map = array(
  59. self::APERTURE => Exif::APERTURE,
  60. self::ARTIST => Exif::AUTHOR,
  61. self::MODEL => Exif::CAMERA,
  62. self::CAPTION => Exif::CAPTION,
  63. self::COLORSPACE => Exif::COLORSPACE,
  64. self::COPYRIGHT => Exif::COPYRIGHT,
  65. self::DATETIMEORIGINAL => Exif::CREATION_DATE,
  66. self::CREDIT => Exif::CREDIT,
  67. self::EXPOSURETIME => Exif::EXPOSURE,
  68. self::FILESIZE => Exif::FILESIZE,
  69. self::FOCALLENGTH => Exif::FOCAL_LENGTH,
  70. self::APPROXIMATEFOCUSDISTANCE => Exif::FOCAL_DISTANCE,
  71. self::HEADLINE => Exif::HEADLINE,
  72. self::IMAGEHEIGHT => Exif::HEIGHT,
  73. self::XRESOLUTION => Exif::HORIZONTAL_RESOLUTION,
  74. self::ISO => Exif::ISO,
  75. self::JOBTITLE => Exif::JOB_TITLE,
  76. self::KEYWORDS => Exif::KEYWORDS,
  77. self::MIMETYPE => Exif::MIMETYPE,
  78. self::ORIENTATION => Exif::ORIENTATION,
  79. self::SOFTWARE => Exif::SOFTWARE,
  80. self::SOURCE => Exif::SOURCE,
  81. self::TITLE => Exif::TITLE,
  82. self::YRESOLUTION => Exif::VERTICAL_RESOLUTION,
  83. self::IMAGEWIDTH => Exif::WIDTH,
  84. self::CAPTIONABSTRACT => Exif::CAPTION,
  85. self::GPSLATITUDE => Exif::GPS,
  86. self::GPSLONGITUDE => Exif::GPS,
  87. );
  88. /**
  89. * @var bool
  90. */
  91. protected $numeric = true;
  92. /**
  93. * Mutator method for the numeric property
  94. *
  95. * @param bool $numeric
  96. * @return \PHPExif\Mapper\Exiftool
  97. */
  98. public function setNumeric($numeric)
  99. {
  100. $this->numeric = (bool) $numeric;
  101. return $this;
  102. }
  103. /**
  104. * Maps the array of raw source data to the correct
  105. * fields for the \PHPExif\Exif class
  106. *
  107. * @param array $data
  108. * @return array
  109. */
  110. public function mapRawData(array $data)
  111. {
  112. $mappedData = array();
  113. $gpsData = array();
  114. foreach ($data as $field => $value) {
  115. if (!array_key_exists($field, $this->map)) {
  116. // silently ignore unknown fields
  117. continue;
  118. }
  119. $key = $this->map[$field];
  120. // manipulate the value if necessary
  121. switch ($field) {
  122. case self::APERTURE:
  123. $value = sprintf('f/%01.1f', $value);
  124. break;
  125. case self::APPROXIMATEFOCUSDISTANCE:
  126. $value = sprintf('%1$sm', $value);
  127. break;
  128. case self::DATETIMEORIGINAL:
  129. try {
  130. $value = new DateTime($value);
  131. } catch (\Exception $exception) {
  132. continue 2;
  133. }
  134. break;
  135. case self::EXPOSURETIME:
  136. // Based on the source code of Exiftool (PrintExposureTime subroutine):
  137. // http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
  138. if ($value < 0.25001 && $value > 0) {
  139. $value = sprintf('1/%d', intval(0.5 + 1 / $value));
  140. } else {
  141. $value = sprintf('%.1f', $value);
  142. $value = preg_replace('/.0$/', '', $value);
  143. }
  144. break;
  145. case self::FOCALLENGTH:
  146. if (!$this->numeric || strpos($value, ' ') !== false) {
  147. $focalLengthParts = explode(' ', $value);
  148. $value = reset($focalLengthParts);
  149. }
  150. break;
  151. case self::GPSLATITUDE:
  152. $gpsData['lat'] = $this->extractGPSCoordinates($value);
  153. break;
  154. case self::GPSLONGITUDE:
  155. $gpsData['lon'] = $this->extractGPSCoordinates($value);
  156. break;
  157. }
  158. // set end result
  159. $mappedData[$key] = $value;
  160. }
  161. // add GPS coordinates, if available
  162. if (count($gpsData) === 2 && $gpsData['lat'] !== false && $gpsData['lon'] !== false) {
  163. $latitudeRef = empty($data['GPS:GPSLatitudeRef'][0]) ? 'N' : $data['GPS:GPSLatitudeRef'][0];
  164. $longitudeRef = empty($data['GPS:GPSLongitudeRef'][0]) ? 'E' : $data['GPS:GPSLongitudeRef'][0];
  165. $gpsLocation = sprintf(
  166. '%s,%s',
  167. (strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
  168. (strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
  169. );
  170. $mappedData[Exif::GPS] = $gpsLocation;
  171. } else {
  172. unset($mappedData[Exif::GPS]);
  173. }
  174. return $mappedData;
  175. }
  176. /**
  177. * Extract GPS coordinates from formatted string
  178. *
  179. * @param string $coordinates
  180. * @return array
  181. */
  182. protected function extractGPSCoordinates($coordinates)
  183. {
  184. if ($this->numeric === true) {
  185. return abs((float) $coordinates);
  186. } else {
  187. if (!preg_match('!^([0-9.]+) deg ([0-9.]+)\' ([0-9.]+)"!', $coordinates, $matches)) {
  188. return false;
  189. }
  190. return intval($matches[1]) + (intval($matches[2]) / 60) + (floatval($matches[3]) / 3600);
  191. }
  192. }
  193. }