IptcCommand.php 2.9 KB

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