Exif.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. <?php
  2. /**
  3. * PHP Exif Reader: Reads EXIF metadata from a file, without having to install additional PHP modules
  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 Exif
  10. */
  11. namespace PHPExif;
  12. /**
  13. * PHP Exif Reader
  14. *
  15. * Responsible for all the read operations on a file's EXIF metadata
  16. *
  17. * @category PHPExif
  18. * @package Exif
  19. * @
  20. */
  21. class Exif
  22. {
  23. const APERTURE = 'aperture';
  24. const AUTHOR = 'author';
  25. const CAMERA = 'camera';
  26. const CAPTION = 'caption';
  27. const COLORSPACE = 'ColorSpace';
  28. const COPYRIGHT = 'copyright';
  29. const CREATION_DATE = 'creationdate';
  30. const CREDIT = 'credit';
  31. const EXPOSURE = 'exposure';
  32. const FILESIZE = 'FileSize';
  33. const FOCAL_LENGTH = 'focalLength';
  34. const FOCAL_DISTANCE = 'focalDistance';
  35. const HEADLINE = 'headline';
  36. const HEIGHT = 'height';
  37. const HORIZONTAL_RESOLUTION = 'horizontalResolution';
  38. const ISO = 'iso';
  39. const JOB_TITLE = 'jobTitle';
  40. const KEYWORDS = 'keywords';
  41. const MIMETYPE = 'MimeType';
  42. const ORIENTATION = 'Orientation';
  43. const SOFTWARE = 'software';
  44. const SOURCE = 'source';
  45. const TITLE = 'title';
  46. const VERTICAL_RESOLUTION = 'verticalResolution';
  47. const WIDTH = 'width';
  48. const GPS = 'gps';
  49. /**
  50. * The mapped EXIF data
  51. *
  52. * @var array
  53. */
  54. protected $data = array();
  55. /**
  56. * The raw EXIF data
  57. *
  58. * @var array
  59. */
  60. protected $rawData = array();
  61. /**
  62. * Class constructor
  63. *
  64. * @param array $data
  65. */
  66. public function __construct(array $data = array())
  67. {
  68. $this->setData($data);
  69. }
  70. /**
  71. * Sets the raw EXIF data
  72. *
  73. * @param array $data The data to set
  74. * @return \PHPExif\Exif Current instance for chaining
  75. */
  76. public function setRawData(array $data)
  77. {
  78. $this->rawData = $data;
  79. return $this;
  80. }
  81. /**
  82. * Returns all EXIF data in the raw original format
  83. *
  84. * @return array
  85. */
  86. public function getRawData()
  87. {
  88. return $this->rawData;
  89. }
  90. /**
  91. * Sets the mapped EXIF data
  92. *
  93. * @param array $data The data to set
  94. * @return \PHPExif\Exif Current instance for chaining
  95. */
  96. public function setData(array $data)
  97. {
  98. $this->data = $data;
  99. return $this;
  100. }
  101. /**
  102. * Returns the mapped EXIF data
  103. *
  104. * @return array
  105. */
  106. public function getData()
  107. {
  108. return $this->data;
  109. }
  110. /**
  111. * Returns the Aperture F-number
  112. *
  113. * @return string|boolean
  114. */
  115. public function getAperture()
  116. {
  117. if (!isset($this->data[self::APERTURE])) {
  118. return false;
  119. }
  120. return $this->data[self::APERTURE];
  121. }
  122. /**
  123. * Sets the Aperture F-number
  124. *
  125. * @param string $value
  126. * @return \PHPExif\Exif
  127. */
  128. public function setAperture($value)
  129. {
  130. $this->data[self::APERTURE] = $value;
  131. return $this;
  132. }
  133. /**
  134. * Returns the Author
  135. *
  136. * @return string|boolean
  137. */
  138. public function getAuthor()
  139. {
  140. if (!isset($this->data[self::AUTHOR])) {
  141. return false;
  142. }
  143. return $this->data[self::AUTHOR];
  144. }
  145. /**
  146. * Sets the Author
  147. *
  148. * @param string $value
  149. * @return \PHPExif\Exif
  150. */
  151. public function setAuthor($value)
  152. {
  153. $this->data[self::AUTHOR] = $value;
  154. return $this;
  155. }
  156. /**
  157. * Returns the Headline
  158. *
  159. * @return string|boolean
  160. */
  161. public function getHeadline()
  162. {
  163. if (!isset($this->data[self::HEADLINE])) {
  164. return false;
  165. }
  166. return $this->data[self::HEADLINE];
  167. }
  168. /**
  169. * Sets the Headline
  170. *
  171. * @param string $value
  172. * @return \PHPExif\Exif
  173. */
  174. public function setHeadline($value)
  175. {
  176. $this->data[self::HEADLINE] = $value;
  177. return $this;
  178. }
  179. /**
  180. * Returns the Credit
  181. *
  182. * @return string|boolean
  183. */
  184. public function getCredit()
  185. {
  186. if (!isset($this->data[self::CREDIT])) {
  187. return false;
  188. }
  189. return $this->data[self::CREDIT];
  190. }
  191. /**
  192. * Sets the Credit
  193. *
  194. * @param string $value
  195. * @return \PHPExif\Exif
  196. */
  197. public function setCredit($value)
  198. {
  199. $this->data[self::CREDIT] = $value;
  200. return $this;
  201. }
  202. /**
  203. * Returns the source
  204. *
  205. * @return string|boolean
  206. */
  207. public function getSource()
  208. {
  209. if (!isset($this->data[self::SOURCE])) {
  210. return false;
  211. }
  212. return $this->data[self::SOURCE];
  213. }
  214. /**
  215. * Sets the Source
  216. *
  217. * @param string $value
  218. * @return \PHPExif\Exif
  219. */
  220. public function setSource($value)
  221. {
  222. $this->data[self::SOURCE] = $value;
  223. return $this;
  224. }
  225. /**
  226. * Returns the Jobtitle
  227. *
  228. * @return string|boolean
  229. */
  230. public function getJobtitle()
  231. {
  232. if (!isset($this->data[self::JOB_TITLE])) {
  233. return false;
  234. }
  235. return $this->data[self::JOB_TITLE];
  236. }
  237. /**
  238. * Sets the Jobtitle
  239. *
  240. * @param string $value
  241. * @return \PHPExif\Exif
  242. */
  243. public function setJobtitle($value)
  244. {
  245. $this->data[self::JOB_TITLE] = $value;
  246. return $this;
  247. }
  248. /**
  249. * Returns the ISO speed
  250. *
  251. * @return int|boolean
  252. */
  253. public function getIso()
  254. {
  255. if (!isset($this->data[self::ISO])) {
  256. return false;
  257. }
  258. return $this->data[self::ISO];
  259. }
  260. /**
  261. * Sets the ISO
  262. *
  263. * @param int $value
  264. * @return \PHPExif\Exif
  265. */
  266. public function setIso($value)
  267. {
  268. $this->data[self::ISO] = $value;
  269. return $this;
  270. }
  271. /**
  272. * Returns the Exposure
  273. *
  274. * @return string|boolean
  275. */
  276. public function getExposure()
  277. {
  278. if (!isset($this->data[self::EXPOSURE])) {
  279. return false;
  280. }
  281. return $this->data[self::EXPOSURE];
  282. }
  283. /**
  284. * Sets the Exposure
  285. *
  286. * @param string $value
  287. * @return \PHPExif\Exif
  288. */
  289. public function setExposure($value)
  290. {
  291. $this->data[self::EXPOSURE] = $value;
  292. return $this;
  293. }
  294. /**
  295. * Returns the Exposure
  296. *
  297. * @return float|boolean
  298. */
  299. public function getExposureMilliseconds()
  300. {
  301. if (!isset($this->data[self::EXPOSURE])) {
  302. return false;
  303. }
  304. if (is_numeric($this->data[self::EXPOSURE])) {
  305. return $this->data[self::EXPOSURE] + 0;
  306. }
  307. $exposureParts = explode('/', $this->data[self::EXPOSURE]);
  308. return (int) reset($exposureParts) / (int) end($exposureParts);
  309. }
  310. /**
  311. * Returns the focus distance, if it exists
  312. *
  313. * @return string|boolean
  314. */
  315. public function getFocusDistance()
  316. {
  317. if (!isset($this->data[self::FOCAL_DISTANCE])) {
  318. return false;
  319. }
  320. return $this->data[self::FOCAL_DISTANCE];
  321. }
  322. /**
  323. * Sets the focus distance
  324. *
  325. * @param string $value
  326. * @return \PHPExif\Exif
  327. */
  328. public function setFocusDistance($value)
  329. {
  330. $this->data[self::FOCAL_DISTANCE] = $value;
  331. return $this;
  332. }
  333. /**
  334. * Returns the width in pixels, if it exists
  335. *
  336. * @return int|boolean
  337. */
  338. public function getWidth()
  339. {
  340. if (!isset($this->data[self::WIDTH])) {
  341. return false;
  342. }
  343. return $this->data[self::WIDTH];
  344. }
  345. /**
  346. * Sets the width
  347. *
  348. * @param int $value
  349. * @return \PHPExif\Exif
  350. */
  351. public function setWidth($value)
  352. {
  353. $this->data[self::WIDTH] = $value;
  354. return $this;
  355. }
  356. /**
  357. * Returns the height in pixels, if it exists
  358. *
  359. * @return int|boolean
  360. */
  361. public function getHeight()
  362. {
  363. if (!isset($this->data[self::HEIGHT])) {
  364. return false;
  365. }
  366. return $this->data[self::HEIGHT];
  367. }
  368. /**
  369. * Sets the height
  370. *
  371. * @param int $value
  372. * @return \PHPExif\Exif
  373. */
  374. public function setHeight($value)
  375. {
  376. $this->data[self::HEIGHT] = $value;
  377. return $this;
  378. }
  379. /**
  380. * Returns the title, if it exists
  381. *
  382. * @return string|boolean
  383. */
  384. public function getTitle()
  385. {
  386. if (!isset($this->data[self::TITLE])) {
  387. return false;
  388. }
  389. return $this->data[self::TITLE];
  390. }
  391. /**
  392. * Sets the title
  393. *
  394. * @param string $value
  395. * @return \PHPExif\Exif
  396. */
  397. public function setTitle($value)
  398. {
  399. $this->data[self::TITLE] = $value;
  400. return $this;
  401. }
  402. /**
  403. * Returns the caption, if it exists
  404. *
  405. * @return string|boolean
  406. */
  407. public function getCaption()
  408. {
  409. if (!isset($this->data[self::CAPTION])) {
  410. return false;
  411. }
  412. return $this->data[self::CAPTION];
  413. }
  414. /**
  415. * Sets the caption
  416. *
  417. * @param string $value
  418. * @return \PHPExif\Exif
  419. */
  420. public function setCaption($value)
  421. {
  422. $this->data[self::CAPTION] = $value;
  423. return $this;
  424. }
  425. /**
  426. * Returns the copyright, if it exists
  427. *
  428. * @return string|boolean
  429. */
  430. public function getCopyright()
  431. {
  432. if (!isset($this->data[self::COPYRIGHT])) {
  433. return false;
  434. }
  435. return $this->data[self::COPYRIGHT];
  436. }
  437. /**
  438. * Sets the copyright
  439. *
  440. * @param string $value
  441. * @return \PHPExif\Exif
  442. */
  443. public function setCopyright($value)
  444. {
  445. $this->data[self::COPYRIGHT] = $value;
  446. return $this;
  447. }
  448. /**
  449. * Returns the keywords, if they exists
  450. *
  451. * @return array|boolean
  452. */
  453. public function getKeywords()
  454. {
  455. if (!isset($this->data[self::KEYWORDS])) {
  456. return false;
  457. }
  458. return $this->data[self::KEYWORDS];
  459. }
  460. /**
  461. * Sets the keywords
  462. *
  463. * @param array $value
  464. * @return \PHPExif\Exif
  465. */
  466. public function setKeywords($value)
  467. {
  468. $this->data[self::KEYWORDS] = $value;
  469. return $this;
  470. }
  471. /**
  472. * Returns the camera, if it exists
  473. *
  474. * @return string|boolean
  475. */
  476. public function getCamera()
  477. {
  478. if (!isset($this->data[self::CAMERA])) {
  479. return false;
  480. }
  481. return $this->data[self::CAMERA];
  482. }
  483. /**
  484. * Sets the camera
  485. *
  486. * @param string $value
  487. * @return \PHPExif\Exif
  488. */
  489. public function setCamera($value)
  490. {
  491. $this->data[self::CAMERA] = $value;
  492. return $this;
  493. }
  494. /**
  495. * Returns the horizontal resolution in DPI, if it exists
  496. *
  497. * @return int|boolean
  498. */
  499. public function getHorizontalResolution()
  500. {
  501. if (!isset($this->data[self::HORIZONTAL_RESOLUTION])) {
  502. return false;
  503. }
  504. return $this->data[self::HORIZONTAL_RESOLUTION];
  505. }
  506. /**
  507. * Sets the horizontal resolution in DPI
  508. *
  509. * @param int $value
  510. * @return \PHPExif\Exif
  511. */
  512. public function setHorizontalResolution($value)
  513. {
  514. $this->data[self::HORIZONTAL_RESOLUTION] = $value;
  515. return $this;
  516. }
  517. /**
  518. * Returns the vertical resolution in DPI, if it exists
  519. *
  520. * @return int|boolean
  521. */
  522. public function getVerticalResolution()
  523. {
  524. if (!isset($this->data[self::VERTICAL_RESOLUTION])) {
  525. return false;
  526. }
  527. return $this->data[self::VERTICAL_RESOLUTION];
  528. }
  529. /**
  530. * Sets the vertical resolution in DPI
  531. *
  532. * @param int $value
  533. * @return \PHPExif\Exif
  534. */
  535. public function setVerticalResolution($value)
  536. {
  537. $this->data[self::VERTICAL_RESOLUTION] = $value;
  538. return $this;
  539. }
  540. /**
  541. * Returns the software, if it exists
  542. *
  543. * @return string|boolean
  544. */
  545. public function getSoftware()
  546. {
  547. if (!isset($this->data[self::SOFTWARE])) {
  548. return false;
  549. }
  550. return $this->data[self::SOFTWARE];
  551. }
  552. /**
  553. * Sets the software
  554. *
  555. * @param string $value
  556. * @return \PHPExif\Exif
  557. */
  558. public function setSoftware($value)
  559. {
  560. $this->data[self::SOFTWARE] = trim($value);
  561. return $this;
  562. }
  563. /**
  564. * Returns the focal length in mm, if it exists
  565. *
  566. * @return float|boolean
  567. */
  568. public function getFocalLength()
  569. {
  570. if (!isset($this->data[self::FOCAL_LENGTH])) {
  571. return false;
  572. }
  573. return $this->data[self::FOCAL_LENGTH];
  574. }
  575. /**
  576. * Sets the focal length in mm
  577. *
  578. * @param float $value
  579. * @return \PHPExif\Exif
  580. */
  581. public function setFocalLength($value)
  582. {
  583. $this->data[self::FOCAL_LENGTH] = $value;
  584. return $this;
  585. }
  586. /**
  587. * Returns the creation datetime, if it exists
  588. *
  589. * @return \DateTime|boolean
  590. */
  591. public function getCreationDate()
  592. {
  593. if (!isset($this->data[self::CREATION_DATE])) {
  594. return false;
  595. }
  596. return $this->data[self::CREATION_DATE];
  597. }
  598. /**
  599. * Sets the creation datetime
  600. *
  601. * @param \DateTime $value
  602. * @return \PHPExif\Exif
  603. */
  604. public function setCreationDate(\DateTime $value)
  605. {
  606. $this->data[self::CREATION_DATE] = $value;
  607. return $this;
  608. }
  609. /**
  610. * Returns the colorspace, if it exists
  611. *
  612. * @return string|boolean
  613. */
  614. public function getColorSpace()
  615. {
  616. if (!isset($this->data[self::COLORSPACE])) {
  617. return false;
  618. }
  619. return $this->data[self::COLORSPACE];
  620. }
  621. /**
  622. * Sets the colorspace
  623. *
  624. * @param string $value
  625. * @return \PHPExif\Exif
  626. */
  627. public function setColorSpace($value)
  628. {
  629. $this->data[self::COLORSPACE] = $value;
  630. return $this;
  631. }
  632. /**
  633. * Returns the mimetype, if it exists
  634. *
  635. * @return string|boolean
  636. */
  637. public function getMimeType()
  638. {
  639. if (!isset($this->data[self::MIMETYPE])) {
  640. return false;
  641. }
  642. return $this->data[self::MIMETYPE];
  643. }
  644. /**
  645. * Sets the mimetype
  646. *
  647. * @param string $value
  648. * @return \PHPExif\Exif
  649. */
  650. public function setMimeType($value)
  651. {
  652. $this->data[self::MIMETYPE] = $value;
  653. return $this;
  654. }
  655. /**
  656. * Returns the filesize, if it exists
  657. *
  658. * @return int|boolean
  659. */
  660. public function getFileSize()
  661. {
  662. if (!isset($this->data[self::FILESIZE])) {
  663. return false;
  664. }
  665. return $this->data[self::FILESIZE];
  666. }
  667. /**
  668. * Sets the filesize
  669. *
  670. * @param int $value
  671. * @return \PHPExif\Exif
  672. */
  673. public function setFileSize($value)
  674. {
  675. $this->data[self::FILESIZE] = $value;
  676. return $this;
  677. }
  678. /**
  679. * Returns the orientation, if it exists
  680. *
  681. * @return int|boolean
  682. */
  683. public function getOrientation()
  684. {
  685. if (!isset($this->data[self::ORIENTATION])) {
  686. return false;
  687. }
  688. return $this->data[self::ORIENTATION];
  689. }
  690. /**
  691. * Sets the orientation
  692. *
  693. * @param int $value
  694. * @return \PHPExif\Exif
  695. */
  696. public function setOrientation($value)
  697. {
  698. $this->data[self::ORIENTATION] = $value;
  699. return $this;
  700. }
  701. /**
  702. * Returns GPS coordinates, if it exists
  703. *
  704. * @return array|boolean
  705. */
  706. public function getGPS()
  707. {
  708. if (!isset($this->data[self::GPS])) {
  709. return false;
  710. }
  711. return $this->data[self::GPS];
  712. }
  713. /**
  714. * Sets the GPS coordinates
  715. *
  716. * @param string $value
  717. * @return \PHPExif\Exif
  718. */
  719. public function setGPS($value)
  720. {
  721. $this->data[self::GPS] = $value;
  722. return $this;
  723. }
  724. }