|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHP Exif Exiftool Reader Adapter |
| 4 | + * |
| 5 | + * @link http://github.com/miljar/PHPExif for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2013 Tom Van Herreweghe <tom@theanalogguy.be> |
| 7 | + * @license http://github.com/miljar/PHPExif/blob/master/LICENSE MIT License |
| 8 | + * @category PHPExif |
| 9 | + * @package Reader |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PHPExif\Reader\Adapter; |
| 13 | + |
| 14 | +use PHPExif\Reader\AdapterAbstract; |
| 15 | +use PHPExif\Exif; |
| 16 | +use \InvalidArgumentException; |
| 17 | +use \RuntimeException; |
| 18 | + |
| 19 | +/** |
| 20 | + * PHP Exif Exiftool Reader Adapter |
| 21 | + * |
| 22 | + * Uses native PHP functionality to read data from a file |
| 23 | + * |
| 24 | + * @category PHPExif |
| 25 | + * @package Reader |
| 26 | + */ |
| 27 | +class Exiftool extends AdapterAbstract |
| 28 | +{ |
| 29 | + const TOOL_NAME = 'exiftool'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Path to the exiftool binary |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $toolPath; |
| 37 | + |
| 38 | + /** |
| 39 | + * Setter for the exiftool binary path |
| 40 | + * |
| 41 | + * @param string $path The path to the exiftool binary |
| 42 | + * @return \PHPExif\Reader\Adapter\Exiftool Current instance |
| 43 | + * @throws \InvalidArgumentException When path is invalid |
| 44 | + */ |
| 45 | + public function setToolPath($path) |
| 46 | + { |
| 47 | + if (!file_exists($path)) { |
| 48 | + throw new InvalidArgumentException( |
| 49 | + sprintf( |
| 50 | + 'Given path (%1$s) to the exiftool binary is invalid', |
| 51 | + $path |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + $this->toolPath = $path; |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Getter for the exiftool binary path |
| 63 | + * Lazy loads the "default" path |
| 64 | + * |
| 65 | + * @return string |
| 66 | + */ |
| 67 | + public function getToolPath() |
| 68 | + { |
| 69 | + if (empty($this->toolPath)) { |
| 70 | + $path = exec('which ' . self::TOOL_NAME); |
| 71 | + $this->setToolPath($path); |
| 72 | + } |
| 73 | + |
| 74 | + return $this->toolPath; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Reads & parses the EXIF data from given file |
| 79 | + * |
| 80 | + * @param string $file |
| 81 | + * @return \PHPExif\Exif Instance of Exif object with data |
| 82 | + * @throws \RuntimeException If the EXIF data could not be read |
| 83 | + */ |
| 84 | + public function getExifFromFile($file) |
| 85 | + { |
| 86 | + $result = $this->getCliOutput( |
| 87 | + sprintf( |
| 88 | + '%1$s -j %2$s', |
| 89 | + $this->getToolPath(), |
| 90 | + $file |
| 91 | + ) |
| 92 | + ); |
| 93 | + |
| 94 | + $data = json_decode($result); |
| 95 | + var_dump($data); |
| 96 | + die(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the output from given cli command |
| 101 | + * |
| 102 | + * @param string $command |
| 103 | + * @return mixed |
| 104 | + * @throws RuntimeException If the command can't be executed |
| 105 | + */ |
| 106 | + protected function getCliOutput($command) |
| 107 | + { |
| 108 | + $descriptorspec = array( |
| 109 | + 0 => array('pipe', 'r'), |
| 110 | + 1 => array('pipe', 'w'), |
| 111 | + 2 => array('pipe', 'a') |
| 112 | + ); |
| 113 | + |
| 114 | + $process = proc_open($command, $descriptorspec, $pipes); |
| 115 | + |
| 116 | + if (!is_resource($process)) { |
| 117 | + throw new RuntimeException( |
| 118 | + 'Could not open a resource to the exiftool binary' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + $result = stream_get_contents($pipes[1]); |
| 123 | + fclose($pipes[0]); |
| 124 | + fclose($pipes[1]); |
| 125 | + fclose($pipes[2]); |
| 126 | + |
| 127 | + proc_close($process); |
| 128 | + |
| 129 | + return $result; |
| 130 | + } |
| 131 | +} |
0 commit comments