ExifCommand.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Intervention\Image\Imagick\Commands;
  3. use Intervention\Image\Commands\ExifCommand as BaseCommand;
  4. class ExifCommand extends BaseCommand
  5. {
  6. /**
  7. * Prefer extension or not
  8. *
  9. * @var bool
  10. */
  11. private $preferExtension = true;
  12. /**
  13. *
  14. */
  15. public function dontPreferExtension() {
  16. $this->preferExtension = false;
  17. }
  18. /**
  19. * Read Exif data from the given image
  20. *
  21. * @param \Intervention\Image\Image $image
  22. * @return boolean
  23. */
  24. public function execute($image)
  25. {
  26. if ($this->preferExtension && function_exists('exif_read_data')) {
  27. return parent::execute($image);
  28. }
  29. $core = $image->getCore();
  30. if ( ! method_exists($core, 'getImageProperties')) {
  31. throw new \Intervention\Image\Exception\NotSupportedException(
  32. "Reading Exif data is not supported by this PHP installation."
  33. );
  34. }
  35. $requestedKey = $this->argument(0)->value();
  36. if ($requestedKey !== null) {
  37. $this->setOutput($core->getImageProperty('exif:' . $requestedKey));
  38. return true;
  39. }
  40. $exif = [];
  41. $properties = $core->getImageProperties();
  42. foreach ($properties as $key => $value) {
  43. if (substr($key, 0, 5) !== 'exif:') {
  44. continue;
  45. }
  46. $exif[substr($key, 5)] = $value;
  47. }
  48. $this->setOutput($exif);
  49. return true;
  50. }
  51. }