IptcCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Intervention\Image\Commands;
  3. class IptcCommand extends AbstractCommand
  4. {
  5. /**
  6. * Read Iptc data from the given image
  7. *
  8. * @param \Intervention\Image\Image $image
  9. * @return boolean
  10. */
  11. public function execute($image)
  12. {
  13. if ( ! function_exists('iptcparse')) {
  14. throw new \Intervention\Image\Exception\NotSupportedException(
  15. "Reading Iptc data is not supported by this PHP installation."
  16. );
  17. }
  18. $key = $this->argument(0)->value();
  19. $info = array();
  20. @getimagesize($image->dirname .'/'. $image->basename, $info);
  21. $data = array();
  22. if (array_key_exists('APP13', $info)) {
  23. $iptc = iptcparse($info['APP13']);
  24. if (is_array($iptc)) {
  25. $data['DocumentTitle'] = isset($iptc["2#005"][0]) ? $iptc["2#005"][0] : null;
  26. $data['Urgency'] = isset($iptc["2#010"][0]) ? $iptc["2#010"][0] : null;
  27. $data['Category'] = isset($iptc["2#015"][0]) ? $iptc["2#015"][0] : null;
  28. $data['Subcategories'] = isset($iptc["2#020"][0]) ? $iptc["2#020"][0] : null;
  29. $data['Keywords'] = isset($iptc["2#025"][0]) ? $iptc["2#025"][0] : null;
  30. $data['SpecialInstructions'] = isset($iptc["2#040"][0]) ? $iptc["2#040"][0] : null;
  31. $data['CreationDate'] = isset($iptc["2#055"][0]) ? $iptc["2#055"][0] : null;
  32. $data['AuthorByline'] = isset($iptc["2#080"][0]) ? $iptc["2#080"][0] : null;
  33. $data['AuthorTitle'] = isset($iptc["2#085"][0]) ? $iptc["2#085"][0] : null;
  34. $data['City'] = isset($iptc["2#090"][0]) ? $iptc["2#090"][0] : null;
  35. $data['State'] = isset($iptc["2#095"][0]) ? $iptc["2#095"][0] : null;
  36. $data['Country'] = isset($iptc["2#101"][0]) ? $iptc["2#101"][0] : null;
  37. $data['OTR'] = isset($iptc["2#103"][0]) ? $iptc["2#103"][0] : null;
  38. $data['Headline'] = isset($iptc["2#105"][0]) ? $iptc["2#105"][0] : null;
  39. $data['Source'] = isset($iptc["2#110"][0]) ? $iptc["2#110"][0] : null;
  40. $data['PhotoSource'] = isset($iptc["2#115"][0]) ? $iptc["2#115"][0] : null;
  41. $data['Copyright'] = isset($iptc["2#116"][0]) ? $iptc["2#116"][0] : null;
  42. $data['Caption'] = isset($iptc["2#120"][0]) ? $iptc["2#120"][0] : null;
  43. $data['CaptionWriter'] = isset($iptc["2#120"][0]) ? $iptc["2#122"][0] : null;
  44. }
  45. }
  46. if (! is_null($key) && is_array($data)) {
  47. $data = array_key_exists($key, $data) ? $data[$key] : false;
  48. }
  49. $this->setOutput($data);
  50. return true;
  51. }
  52. }