Native.php 5.6 KB

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