Native.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * PHP Exif Native 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. use Exception;
  15. /**
  16. * PHP Exif Native Mapper
  17. *
  18. * Maps native raw data to valid data for the \PHPExif\Exif class
  19. *
  20. * @category PHPExif
  21. * @package Mapper
  22. */
  23. class Native implements MapperInterface
  24. {
  25. const APERTUREFNUMBER = 'ApertureFNumber';
  26. const ARTIST = 'Artist';
  27. const CAPTION = 'caption';
  28. const COLORSPACE = 'ColorSpace';
  29. const COPYRIGHT = 'copyright';
  30. const DATETIMEORIGINAL = 'DateTimeOriginal';
  31. const CREDIT = 'credit';
  32. const EXPOSURETIME = 'ExposureTime';
  33. const FILESIZE = 'FileSize';
  34. const FOCALLENGTH = 'FocalLength';
  35. const FOCUSDISTANCE = 'FocusDistance';
  36. const HEADLINE = 'headline';
  37. const HEIGHT = 'Height';
  38. const ISOSPEEDRATINGS = 'ISOSpeedRatings';
  39. const JOBTITLE = 'jobtitle';
  40. const KEYWORDS = 'keywords';
  41. const MIMETYPE = 'MimeType';
  42. const MODEL = 'Model';
  43. const ORIENTATION = 'Orientation';
  44. const SOFTWARE = 'Software';
  45. const SOURCE = 'source';
  46. const TITLE = 'title';
  47. const WIDTH = 'Width';
  48. const XRESOLUTION = 'XResolution';
  49. const YRESOLUTION = 'YResolution';
  50. const GPSLATITUDE = 'GPSLatitude';
  51. const GPSLONGITUDE = 'GPSLongitude';
  52. const SECTION_FILE = 'FILE';
  53. const SECTION_COMPUTED = 'COMPUTED';
  54. const SECTION_IFD0 = 'IFD0';
  55. const SECTION_THUMBNAIL = 'THUMBNAIL';
  56. const SECTION_COMMENT = 'COMMENT';
  57. const SECTION_EXIF = 'EXIF';
  58. const SECTION_ALL = 'ANY_TAG';
  59. const SECTION_IPTC = 'IPTC';
  60. /**
  61. * A list of section names
  62. *
  63. * @var array
  64. */
  65. protected $sections = array(
  66. self::SECTION_FILE,
  67. self::SECTION_COMPUTED,
  68. self::SECTION_IFD0,
  69. self::SECTION_THUMBNAIL,
  70. self::SECTION_COMMENT,
  71. self::SECTION_EXIF,
  72. self::SECTION_ALL,
  73. self::SECTION_IPTC,
  74. );
  75. /**
  76. * Maps the ExifTool fields to the fields of
  77. * the \PHPExif\Exif class
  78. *
  79. * @var array
  80. */
  81. protected $map = array(
  82. self::APERTUREFNUMBER => Exif::APERTURE,
  83. self::FOCUSDISTANCE => Exif::FOCAL_DISTANCE,
  84. self::HEIGHT => Exif::HEIGHT,
  85. self::WIDTH => Exif::WIDTH,
  86. self::CAPTION => Exif::CAPTION,
  87. self::COPYRIGHT => Exif::COPYRIGHT,
  88. self::CREDIT => Exif::CREDIT,
  89. self::HEADLINE => Exif::HEADLINE,
  90. self::JOBTITLE => Exif::JOB_TITLE,
  91. self::KEYWORDS => Exif::KEYWORDS,
  92. self::SOURCE => Exif::SOURCE,
  93. self::TITLE => Exif::TITLE,
  94. self::ARTIST => Exif::AUTHOR,
  95. self::MODEL => Exif::CAMERA,
  96. self::COLORSPACE => Exif::COLORSPACE,
  97. self::DATETIMEORIGINAL => Exif::CREATION_DATE,
  98. self::EXPOSURETIME => Exif::EXPOSURE,
  99. self::FILESIZE => Exif::FILESIZE,
  100. self::FOCALLENGTH => Exif::FOCAL_LENGTH,
  101. self::ISOSPEEDRATINGS => Exif::ISO,
  102. self::MIMETYPE => Exif::MIMETYPE,
  103. self::ORIENTATION => Exif::ORIENTATION,
  104. self::SOFTWARE => Exif::SOFTWARE,
  105. self::XRESOLUTION => Exif::HORIZONTAL_RESOLUTION,
  106. self::YRESOLUTION => Exif::VERTICAL_RESOLUTION,
  107. self::GPSLATITUDE => Exif::GPS,
  108. self::GPSLONGITUDE => Exif::GPS,
  109. );
  110. /**
  111. * Maps the array of raw source data to the correct
  112. * fields for the \PHPExif\Exif class
  113. *
  114. * @param array $data
  115. * @return array
  116. */
  117. public function mapRawData(array $data)
  118. {
  119. $mappedData = array();
  120. $gpsData = array();
  121. foreach ($data as $field => $value) {
  122. if ($this->isSection($field) && is_array($value)) {
  123. $subData = $this->mapRawData($value);
  124. $mappedData = array_merge($mappedData, $subData);
  125. continue;
  126. }
  127. if (!$this->isFieldKnown($field)) {
  128. // silently ignore unknown fields
  129. continue;
  130. }
  131. $key = $this->map[$field];
  132. // manipulate the value if necessary
  133. switch ($field) {
  134. case self::DATETIMEORIGINAL:
  135. try {
  136. $value = new DateTime($value);
  137. } catch (Exception $exception) {
  138. continue 2;
  139. }
  140. break;
  141. case self::EXPOSURETIME:
  142. if (!is_float($value)) {
  143. $value = $this->normalizeComponent($value);
  144. }
  145. // Based on the source code of Exiftool (PrintExposureTime subroutine):
  146. // http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
  147. if ($value < 0.25001 && $value > 0) {
  148. $value = sprintf('1/%d', intval(0.5 + 1 / $value));
  149. } else {
  150. $value = sprintf('%.1f', $value);
  151. $value = preg_replace('/.0$/', '', $value);
  152. }
  153. break;
  154. case self::FOCALLENGTH:
  155. $parts = explode('/', $value);
  156. // Avoid division by zero if focal length is invalid
  157. if (end($parts) == '0') {
  158. $value = 0;
  159. } else {
  160. $value = (int) reset($parts) / (int) end($parts);
  161. }
  162. break;
  163. case self::XRESOLUTION:
  164. case self::YRESOLUTION:
  165. $resolutionParts = explode('/', $value);
  166. $value = (int) reset($resolutionParts);
  167. break;
  168. case self::GPSLATITUDE:
  169. $gpsData['lat'] = $this->extractGPSCoordinate($value);
  170. break;
  171. case self::GPSLONGITUDE:
  172. $gpsData['lon'] = $this->extractGPSCoordinate($value);
  173. break;
  174. }
  175. // set end result
  176. $mappedData[$key] = $value;
  177. }
  178. // add GPS coordinates, if available
  179. if (count($gpsData) === 2) {
  180. $latitudeRef = empty($data['GPSLatitudeRef'][0]) ? 'N' : $data['GPSLatitudeRef'][0];
  181. $longitudeRef = empty($data['GPSLongitudeRef'][0]) ? 'E' : $data['GPSLongitudeRef'][0];
  182. $gpsLocation = sprintf(
  183. '%s,%s',
  184. (strtoupper($latitudeRef) === 'S' ? -1 : 1) * $gpsData['lat'],
  185. (strtoupper($longitudeRef) === 'W' ? -1 : 1) * $gpsData['lon']
  186. );
  187. $mappedData[Exif::GPS] = $gpsLocation;
  188. } else {
  189. unset($mappedData[Exif::GPS]);
  190. }
  191. return $mappedData;
  192. }
  193. /**
  194. * Determines if given field is a section
  195. *
  196. * @param string $field
  197. * @return bool
  198. */
  199. protected function isSection($field)
  200. {
  201. return (in_array($field, $this->sections));
  202. }
  203. /**
  204. * Determines if the given field is known,
  205. * in a case insensitive way for its first letter.
  206. * Also update $field to keep it valid against the known fields.
  207. *
  208. * @param string &$field
  209. * @return bool
  210. */
  211. protected function isFieldKnown(&$field)
  212. {
  213. $lcfField = lcfirst($field);
  214. if (array_key_exists($lcfField, $this->map)) {
  215. $field = $lcfField;
  216. return true;
  217. }
  218. $ucfField = ucfirst($field);
  219. if (array_key_exists($ucfField, $this->map)) {
  220. $field = $ucfField;
  221. return true;
  222. }
  223. return false;
  224. }
  225. /**
  226. * Extract GPS coordinates from components array
  227. *
  228. * @param array|string $components
  229. * @return float
  230. */
  231. protected function extractGPSCoordinate($components)
  232. {
  233. if (!is_array($components)) {
  234. $components = array($components);
  235. }
  236. $components = array_map(array($this, 'normalizeComponent'), $components);
  237. if (count($components) > 2) {
  238. return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
  239. }
  240. return reset($components);
  241. }
  242. /**
  243. * Normalize component
  244. *
  245. * @param mixed $component
  246. * @return int|float
  247. */
  248. protected function normalizeComponent($component)
  249. {
  250. $parts = explode('/', $component);
  251. if (count($parts) > 1) {
  252. if ($parts[1]) {
  253. return intval($parts[0]) / intval($parts[1]);
  254. }
  255. return 0;
  256. }
  257. return floatval(reset($parts));
  258. }
  259. }