Skip to content

Instantly share code, notes, and snippets.

@Trainmaster
Last active January 2, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trainmaster/8361856 to your computer and use it in GitHub Desktop.
Save Trainmaster/8361856 to your computer and use it in GitHub Desktop.
ImageMagickCommandLineProcessor for processing uploaded image files
<?php
/**
* ImagickProcessor
*
* @author Frank Liepert <contact@frank-liepert.de>
*/
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