Exiftool.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PHPExiftool;
  11. use PHPExiftool\Exception\RuntimeException;
  12. use Psr\Log\LoggerAwareInterface;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\Process\Process;
  15. class Exiftool implements LoggerAwareInterface
  16. {
  17. private $logger;
  18. private $binaryPath;
  19. public function __construct(LoggerInterface $logger, $binaryPath = null)
  20. {
  21. $this->logger = $logger;
  22. $this->binaryPath = $binaryPath;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function setLogger(LoggerInterface $logger)
  28. {
  29. $this->logger = $logger;
  30. return $this;
  31. }
  32. /**
  33. * Execute a command and return the output
  34. *
  35. * @param string $command
  36. * @param int $timeout
  37. * @return string
  38. * @throws \Exception
  39. */
  40. public function executeCommand($command, $timeout = 60)
  41. {
  42. $command = ($this->binaryPath == null? self::getBinary(): $this->binaryPath) . ' ' . $command;
  43. $process = new Process($command);
  44. $process->setTimeout($timeout);
  45. $this->logger->addInfo(sprintf('Exiftool executes command %s', $process->getCommandLine()));
  46. $process->run();
  47. if ( ! $process->isSuccessful()) {
  48. throw new RuntimeException(sprintf('Command %s failed : %s, exitcode %s', $command, $process->getErrorOutput(), $process->getExitCode()));
  49. }
  50. $output = $process->getOutput();
  51. unset($process);
  52. return $output;
  53. }
  54. /**
  55. *
  56. * @return string
  57. */
  58. protected static function getBinary()
  59. {
  60. static $binary = null;
  61. if ($binary) {
  62. return $binary;
  63. }
  64. $dev = __DIR__ . '/../../vendor/phpexiftool/exiftool/exiftool';
  65. $packaged = __DIR__ . '/../../../../phpexiftool/exiftool/exiftool';
  66. foreach (array($packaged, $dev) as $location) {
  67. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  68. $location .= '.exe';
  69. }
  70. if (is_executable($location)) {
  71. return $binary = realpath($location);
  72. }
  73. }
  74. throw new RuntimeException('Unable to get exiftool binary');
  75. }
  76. }