Exiftool.php

Go to the documentation of this file.
00001 <?php
00002 
00034 class Robo47_Exiftool
00035 {
00039     const FORMAT_ARRAY = 'array';
00040 
00044     const FORMAT_XML = 'xml';
00045 
00049     const FORMAT_JSON = 'json';
00050 
00055     protected $_format = null;
00061     protected $_exiftool = '/usr/bin/exiftool';
00062 
00067     public function __construct($exiftool = '/usr/bin/exiftool',
00068         $format = self::FORMAT_JSON)
00069     {
00070         $this->setExiftool($exiftool);
00071         $this->setFormat($format);
00072     }
00073 
00080     public function setExiftool($exiftool)
00081     {
00082         $this->_exiftool = $exiftool;
00083         return $this;
00084     }
00085 
00090     public function getExiftool()
00091     {
00092         return $this->_exiftool;
00093     }
00094 
00100     public function setFormat($format)
00101     {
00102         $format = strtolower($format);
00103         switch ($format) {
00104             case self::FORMAT_JSON:
00105             case self::FORMAT_ARRAY:
00106             case self::FORMAT_XML:
00107                 break;
00108             default:
00109                 $message = 'Invalid format: ' . $format;
00110                 throw new Robo47_Exiftool_Exception($message);
00111         }
00112         $this->_format = $format;
00113         return $this;
00114     }
00115 
00120     public function getFormat()
00121     {
00122         return $this->_format;
00123     }
00124 
00133     public function getExifs($file, $format = null)
00134     {
00135         if (!file_exists($file) || !is_file($file)) {
00136             $message = 'File "' . $file . '" does not exist.';
00137             throw new Robo47_Exiftool_Exception($message);
00138         }
00139         if (null === $format) {
00140             $format = $this->getFormat();
00141         }
00142         switch ($format) {
00143             case self::FORMAT_JSON:
00144                 $exifs = $this->_runExiftool($file, self::FORMAT_JSON);
00145                 break;
00146             case self::FORMAT_ARRAY:
00147                 $exifs = $this->_runExiftool($file, self::FORMAT_JSON);
00148                 $exifs = json_decode($exifs, true);
00149                 break;
00150             case self::FORMAT_XML:
00151                 $exifs = $this->_runExiftool($file, self::FORMAT_XML);
00152                 break;
00153         }
00154         return $exifs;
00155     }
00156 
00163     protected function _runExiftool($file, $format)
00164     {
00165         $command = escapeshellarg($this->_exiftool);
00166         switch ($format) {
00167             case self::FORMAT_JSON:
00168                 $command .= ' -j ';
00169                 break;
00170             case self::FORMAT_XML:
00171                 $command .= ' -X ';
00172                 break;
00173         }
00174         $output = array();
00175         $command .= ' ' . escapeshellarg($file);
00176         $command .= ' 2>&1 ';
00177         $returnCode = 0;
00178         exec($command, $output, $returnCode);
00179         if ($returnCode != 0) {
00180             $message = 'executing exiftool failed: ' . implode('', $output);
00181             $message .= PHP_EOL . 'command: ' . $command;
00182             throw new Robo47_Exiftool_Exception($message, $returnCode);
00183         }
00184         return implode('', $output);
00185     }
00186 }