ExifCommand.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Intervention\Image\Commands;
  3. use Intervention\Image\Exception\NotReadableException;
  4. use Intervention\Image\Exception\NotSupportedException;
  5. class ExifCommand extends AbstractCommand
  6. {
  7. /**
  8. * Read Exif data from the given image
  9. *
  10. * Note: Windows PHP Users - in order to use this method you will need to
  11. * enable the mbstring and exif extensions within the php.ini file.
  12. *
  13. * @param \Intervention\Image\Image $image
  14. * @return boolean
  15. */
  16. public function execute($image)
  17. {
  18. if (!function_exists('exif_read_data')) {
  19. throw new NotSupportedException(
  20. "Reading Exif data is not supported by this PHP installation."
  21. );
  22. }
  23. $key = $this->argument(0)->value();
  24. // try to read exif data from image file
  25. try {
  26. $data = @exif_read_data($image->dirname . '/' . $image->basename);
  27. if (!is_null($key) && is_array($data)) {
  28. $data = array_key_exists($key, $data) ? $data[$key] : false;
  29. }
  30. } catch (\Exception $e) {
  31. throw new NotReadableException(
  32. sprintf(
  33. "Cannot read the Exif data from the filename (%s) provided ",
  34. $image->dirname . '/' . $image->basename
  35. ),
  36. $e->getCode(),
  37. $e
  38. );
  39. }
  40. $this->setOutput($data);
  41. return true;
  42. }
  43. }