Dependencies:
Make sure the Tinify library is within PHP's include path.
Dependencies:
Make sure the Tinify library is within PHP's include path.
| <?php | |
| // @see https://gist.github.com/dfelton/5c1e46d3753c31ce9b5899023f0afa24 | |
| require_once 'abstract.php'; | |
| /** | |
| * Tinify shell script | |
| * | |
| * @category FirstScribe | |
| * @package FirstScribe_Shell | |
| * @author Darren D Felton <[email protected]> | |
| */ | |
| class FirstScribe_Shell_Tinify extends FirstScribe_Shell_Abstract | |
| { | |
| /** | |
| * Overwrite existing image or put in subdir | |
| * | |
| * @var bool | |
| */ | |
| protected $_overwrite = false; | |
| /** | |
| * Supported file types | |
| * | |
| * @var array | |
| */ | |
| protected $_supported = array( | |
| IMAGETYPE_JPEG, | |
| IMAGETYPE_PNG | |
| ); | |
| /** | |
| * Run the script | |
| */ | |
| public function run() | |
| { | |
| try { | |
| switch (true) { | |
| case $this->getArg('dir'): | |
| $this->tinifyDir($this->getArg('dir')); | |
| break; | |
| case $this->getArg('image'): | |
| $this->tinifyImage($this->getArg('image')); | |
| break; | |
| default: | |
| die($this->usageHelp()); | |
| break; | |
| } | |
| } catch (Exception $e) { | |
| $this->outputLine($e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Initialize Tinify API | |
| */ | |
| protected function _construct() | |
| { | |
| parent::_construct(); | |
| // @see https://github.com/tinify/tinify-php | |
| require_once 'Tinify/Exception.php'; | |
| require_once 'Tinify/ResultMeta.php'; | |
| require_once 'Tinify/Result.php'; | |
| require_once 'Tinify/Source.php'; | |
| require_once 'Tinify/Client.php'; | |
| require_once 'Tinify.php'; | |
| $apiKey = $this->getArg('key'); | |
| if (!is_string($apiKey)) { | |
| if (!isset($this->_args['h']) && !isset($this->_args['help'])) { | |
| $this->outputLine('ERROR: No API key supplied.'); | |
| die($this->usageHelp()); | |
| } | |
| } | |
| \Tinify\setKey($this->getArg('key')); | |
| if ($this->getArg('overwrite')) { | |
| $this->_overwrite = true; | |
| } | |
| } | |
| /** | |
| * @param string $dir | |
| */ | |
| protected function tinifyDir($dir) | |
| { | |
| switch (true) { | |
| case !is_dir($dir): throw new Exception("ERROR: Invalid directory path supplied \"{$dir}\""); | |
| case !is_readable($dir): throw new Exception("ERROR: Directory not readable \"{$dir}\""); | |
| } | |
| foreach (scandir($dir) as $file) { | |
| $filename = $dir.DS.$file; | |
| $isDir = in_array($file, array('.', '..')); | |
| if (!$isDir && !is_dir($filename) && $this->isSupported($filename)) { | |
| try { | |
| $this->tinifyImage($filename); | |
| } catch (Exception $e) { | |
| $this->outputLine($e->getMessage()); | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Is the file a supported type. | |
| * | |
| * @param string $file | |
| * @return boolean | |
| */ | |
| public function isSupported($file) | |
| { | |
| return in_array(exif_imagetype($file), $this->_supported); | |
| } | |
| /** | |
| * @param string $img | |
| */ | |
| protected function tinifyImage($img) | |
| { | |
| /* @var $source \Tinify\Source */ | |
| switch (true) { | |
| case !is_file($img): throw new Exception("ERROR: Not a file \"{$img}\""); | |
| case !is_readable($img): throw new Exception("ERROR: File readable \"{$img}\""); | |
| case !$this->isSupported($img): throw new Exception("ERROR: Unsupported file type \"{$img}\""); | |
| } | |
| $source = \Tinify\fromFile($img); | |
| $toFile = $img; | |
| if ($this->_overwrite) { | |
| if (!is_writable($img)) { | |
| throw new Exception("ERROR: File not writable \"{$img}\""); | |
| } | |
| } else { | |
| $tinyDir = pathinfo($img, PATHINFO_DIRNAME).DS.'tinified'; | |
| $toFile = $tinyDir.DS.basename($img); | |
| if (!is_dir($tinyDir) && !mkdir($tinyDir)) { | |
| throw new Exception("ERROR: Unable to create directory \"{$tinyDir}\""); | |
| } | |
| } | |
| if (false === $source->toFile($toFile)) { | |
| throw new Exception("ERROR: An unkown problem occurred tinifying file \"{$toFile}\""); | |
| } | |
| $this->outputLine("SUCCESS: Tinified file \"{$toFile}\""); | |
| } | |
| /** | |
| * Output line to screen. | |
| * | |
| * @param string $line | |
| */ | |
| protected function outputLine($line) | |
| { | |
| echo $line,PHP_EOL; | |
| } | |
| /** | |
| * Retrieve Usage Help Message | |
| */ | |
| public function usageHelp() | |
| { | |
| return <<<USAGE | |
| Usage: php -f tinify.php -- [options] | |
| -h Short alias for help | |
| help This help | |
| key API Key to use (required) | |
| dir Directory to tinify (optional) | |
| image Image to tinify (optional. If dir is already supplied, image is ignored) | |
| overwrite Overwrite original (default will put new copy into 'tinified' subdir) | |
| USAGE; | |
| } | |
| } | |
| $shell = new FirstScribe_Shell_Tinify(); | |
| $shell->run(); |