Native.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * PHP Exif Native Reader Adapter
  4. *
  5. * @link http://github.com/miljar/PHPExif for the canonical source repository
  6. * @copyright Copyright (c) 2013 Tom Van Herreweghe <tom@theanalogguy.be>
  7. * @license http://github.com/miljar/PHPExif/blob/master/LICENSE MIT License
  8. * @category PHPExif
  9. * @package Reader
  10. */
  11. namespace PHPExif\Adapter;
  12. use PHPExif\Exif;
  13. use DateTime;
  14. /**
  15. * PHP Exif Native Reader Adapter
  16. *
  17. * Uses native PHP functionality to read data from a file
  18. *
  19. * @category PHPExif
  20. * @package Reader
  21. */
  22. class Native extends AdapterAbstract
  23. {
  24. const INCLUDE_THUMBNAIL = true;
  25. const NO_THUMBNAIL = false;
  26. const SECTIONS_AS_ARRAYS = true;
  27. const SECTIONS_FLAT = false;
  28. const SECTION_FILE = 'FILE';
  29. const SECTION_COMPUTED = 'COMPUTED';
  30. const SECTION_IFD0 = 'IFD0';
  31. const SECTION_THUMBNAIL = 'THUMBNAIL';
  32. const SECTION_COMMENT = 'COMMENT';
  33. const SECTION_EXIF = 'EXIF';
  34. const SECTION_ALL = 'ANY_TAG';
  35. const SECTION_IPTC = 'IPTC';
  36. /**
  37. * List of EXIF sections
  38. *
  39. * @var array
  40. */
  41. protected $requiredSections = array();
  42. /**
  43. * Include the thumbnail in the EXIF data?
  44. *
  45. * @var boolean
  46. */
  47. protected $includeThumbnail = self::NO_THUMBNAIL;
  48. /**
  49. * Parse the sections as arrays?
  50. *
  51. * @var boolean
  52. */
  53. protected $sectionsAsArrays = self::SECTIONS_FLAT;
  54. /**
  55. * @var string
  56. */
  57. protected $mapperClass = '\\PHPExif\\Mapper\\Native';
  58. /**
  59. * Contains the mapping of names to IPTC field numbers
  60. *
  61. * @var array
  62. */
  63. protected $iptcMapping = array(
  64. 'title' => '2#005',
  65. 'keywords' => '2#025',
  66. 'copyright' => '2#116',
  67. 'caption' => '2#120',
  68. 'headline' => '2#105',
  69. 'credit' => '2#110',
  70. 'source' => '2#115',
  71. 'jobtitle' => '2#085'
  72. );
  73. /**
  74. * Getter for the EXIF sections
  75. *
  76. * @return array
  77. */
  78. public function getRequiredSections()
  79. {
  80. return $this->requiredSections;
  81. }
  82. /**
  83. * Setter for the EXIF sections
  84. *
  85. * @param array $sections List of EXIF sections
  86. * @return \PHPExif\Reader Current instance for chaining
  87. */
  88. public function setRequiredSections(array $sections)
  89. {
  90. $this->requiredSections = $sections;
  91. return $this;
  92. }
  93. /**
  94. * Adds an EXIF section to the list
  95. *
  96. * @param string $section
  97. * @return \PHPExif\Reader Current instance for chaining
  98. */
  99. public function addRequiredSection($section)
  100. {
  101. if (!in_array($section, $this->requiredSections)) {
  102. array_push($this->requiredSections, $section);
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Define if the thumbnail should be included into the EXIF data or not
  108. *
  109. * @param boolean $value
  110. * @return \PHPExif\Reader Current instance for chaining
  111. */
  112. public function setIncludeThumbnail($value)
  113. {
  114. $this->includeThumbnail = $value;
  115. return $this;
  116. }
  117. /**
  118. * Returns if the thumbnail should be included into the EXIF data or not
  119. *
  120. * @return boolean
  121. */
  122. public function getIncludeThumbnail()
  123. {
  124. return $this->includeThumbnail;
  125. }
  126. /**
  127. * Define if the sections should be parsed as arrays
  128. *
  129. * @param boolean $value
  130. * @return \PHPExif\Reader Current instance for chaining
  131. */
  132. public function setSectionsAsArrays($value)
  133. {
  134. $this->sectionsAsArrays = (bool) $value;
  135. return $this;
  136. }
  137. /**
  138. * Returns if the sections should be parsed as arrays
  139. *
  140. * @return boolean
  141. */
  142. public function getSectionsAsArrays()
  143. {
  144. return $this->sectionsAsArrays;
  145. }
  146. /**
  147. * Reads & parses the EXIF data from given file
  148. *
  149. * @param string $file
  150. * @return \PHPExif\Exif|boolean Instance of Exif object with data
  151. */
  152. public function getExifFromFile($file)
  153. {
  154. $sections = $this->getRequiredSections();
  155. $sections = implode(',', $sections);
  156. $sections = (empty($sections)) ? null : $sections;
  157. $data = @exif_read_data(
  158. $file,
  159. $sections,
  160. $this->getSectionsAsArrays(),
  161. $this->getIncludeThumbnail()
  162. );
  163. if (false === $data) {
  164. return false;
  165. }
  166. $xmpData = $this->getIptcData($file);
  167. $data = array_merge($data, array(self::SECTION_IPTC => $xmpData));
  168. // map the data:
  169. $mapper = $this->getMapper();
  170. $mappedData = $mapper->mapRawData($data);
  171. // hydrate a new Exif object
  172. $exif = new Exif();
  173. $hydrator = $this->getHydrator();
  174. $hydrator->hydrate($exif, $mappedData);
  175. $exif->setRawData($data);
  176. return $exif;
  177. }
  178. /**
  179. * Returns an array of IPTC data
  180. *
  181. * @param string $file The file to read the IPTC data from
  182. * @return array
  183. */
  184. public function getIptcData($file)
  185. {
  186. getimagesize($file, $info);
  187. $arrData = array();
  188. if (isset($info['APP13'])) {
  189. $iptc = iptcparse($info['APP13']);
  190. foreach ($this->iptcMapping as $name => $field) {
  191. if (!isset($iptc[$field])) {
  192. continue;
  193. }
  194. if (count($iptc[$field]) === 1) {
  195. $arrData[$name] = reset($iptc[$field]);
  196. } else {
  197. $arrData[$name] = $iptc[$field];
  198. }
  199. }
  200. }
  201. return $arrData;
  202. }
  203. }