Last active
January 2, 2016 21:09
-
-
Save Trainmaster/8361856 to your computer and use it in GitHub Desktop.
ImageMagickCommandLineProcessor for processing uploaded image files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ImagickProcessor | |
* | |
* @author Frank Liepert <[email protected]> | |
*/ | |
class ImageMagickCommandLineProcessor | |
{ | |
/** @type string $error */ | |
protected $error; | |
/** @type string $resize */ | |
protected $resize; | |
/** | |
* @api | |
* | |
* @param string $resize | |
* | |
* @return $this Provides a fluent interface. | |
*/ | |
public function setResize($resize) | |
{ | |
$this->resize = trim($resize); | |
return $this; | |
} | |
/** | |
* @api | |
* | |
* @param string $src | |
* @param string $dest | |
* | |
* @return string|false | |
*/ | |
public function process($src, $dest) | |
{ | |
$src = realpath($src); | |
if ($src === false) { | |
return false; | |
} | |
$command = $this->createCommand(array('identify', $src)); | |
exec($command, $output, $returnVar); | |
if ($returnVar !== 0) { | |
$this->error = 'IDENTIFY_ERROR'; | |
return false; | |
} | |
$command = $this->createCommand(array('convert', $src, $this->resize, $dest)); | |
exec($command, $output, $returnVar); | |
if ($returnVar !== 0) { | |
$this->error = 'CONVERT_ERROR'; | |
return false; | |
} | |
return $dest; | |
} | |
/** | |
* @api | |
* | |
* @return string | |
*/ | |
public function getError() | |
{ | |
return $this->error; | |
} | |
/** | |
* @internal | |
* | |
* @param array $commands | |
* | |
* @return string | |
*/ | |
protected function createCommand(array $commands) | |
{ | |
return implode(' ', array_filter($commands)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment