From 5300ffadf1c7faf3f505a585f2223fb6448b86bf Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 3 Aug 2013 20:34:59 -0400 Subject: Better image processing. Add support for GraphicsMagick (a fork of ImageMagick) and `exiftool` (for stripping EXIF metadata quickly). --- inc/image.php | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'inc/image.php') diff --git a/inc/image.php b/inc/image.php index 88f735dd..1dac4724 100644 --- a/inc/image.php +++ b/inc/image.php @@ -19,7 +19,7 @@ class Image { if ($config['thumb_method'] == 'imagick') { $classname = 'ImageImagick'; - } elseif ($config['thumb_method'] == 'convert' || $config['thumb_method'] == 'convert+gifsicle') { + } elseif (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) { $classname = 'ImageConvert'; } else { $classname = 'Image' . strtoupper($this->format); @@ -44,8 +44,6 @@ class Image { public function resize($extension, $max_width, $max_height) { global $config; - - $gifsicle = false; if ($config['thumb_method'] == 'imagick') { $classname = 'ImageImagick'; @@ -54,6 +52,13 @@ class Image { } elseif ($config['thumb_method'] == 'convert+gifsicle') { $classname = 'ImageConvert'; $gifsicle = true; + } elseif ($config['thumb_method'] == 'gm') { + $classname = 'ImageConvert'; + $gm = true; + } elseif ($config['thumb_method'] == 'gm+gifsicle') { + $classname = 'ImageConvert'; + $gm = true; + $gifsicle = true; } else { $classname = 'Image' . strtoupper($extension); if (!class_exists($classname)) { @@ -81,7 +86,6 @@ class Image { $height = $max_height; } - $thumb->gifsicle = $gifsicle; $thumb->_resize($this->image->image, $width, $height); return $thumb; @@ -227,15 +231,20 @@ class ImageImagick extends ImageBase { class ImageConvert extends ImageBase { - public $width, $height, $temp, $gifsicle; + public $width, $height, $temp, $gm = false, $gifsicle = false; public function init() { global $config; + if ($config['thumb_method'] == 'gm' || $config['thumb_method'] == 'gm+gifsicle') + $this->gm = true; + if ($config['thumb_method'] == 'convert+gifsicle' || $config['thumb_method'] == 'gm+gifsicle') + $this->gifsicle = true; + $this->temp = false; } - public function from() { - $size = trim(shell_exec('identify -format "%w %h" ' . escapeshellarg($this->src . '[0]'))); + public function from() { + $size = shell_exec_error(($this->gm ? 'gm ' : '') . 'identify -format "%w %h" ' . escapeshellarg($this->src . '[0]')); if (preg_match('/^(\d+) (\d+)$/', $size, $m)) { $this->width = $m[1]; $this->height = $m[2]; @@ -251,9 +260,17 @@ class ImageConvert extends ImageBase { if (!$this->temp) { if ($config['strip_exif']) { - shell_exec('convert ' . escapeshellarg($this->src) . ' -auto-orient -strip ' . escapeshellarg($src)); + if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' . + escapeshellarg($this->src) . ' -auto-orient -strip ' . escapeshellarg($src))) { + $this->destroy(); + error('Failed to resize image!', null, $error); + } } else { - shell_exec('convert ' . escapeshellarg($this->src) . ' -auto-orient ' . escapeshellarg($src)); + if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' . + escapeshellarg($this->src) . ' -auto-orient ' . escapeshellarg($src))) { + $this->destroy(); + error('Failed to resize image!', null, $error); + } } } else { rename($this->temp, $src); @@ -289,13 +306,25 @@ class ImageConvert extends ImageBase { escapeshellarg($this->temp) . '2>&1 &&echo $?') !== '0') || !file_exists($this->temp)) error('Failed to resize image!', null, $error); } else { - if (trim($error = shell_exec('convert ' . sprintf($config['convert_args'], '', $this->width, $this->height) . ' ' . - escapeshellarg($this->src) . ' ' . escapeshellarg($this->temp) . ' 2>&1 &&echo $?')) !== '0' || !file_exists($this->temp)) + if ($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' . + sprintf($config['convert_args'], + $this->width, + $this->height, + escapeshellarg($this->src), + $this->width, + $this->height, + escapeshellarg($this->temp))) || !file_exists($this->temp)) error('Failed to resize image!', null, $error); } } else { - if (trim($error = shell_exec('convert ' . sprintf($config['convert_args'], '-flatten', $this->width, $this->height) . ' ' . - escapeshellarg($this->src . '[0]') . " " . escapeshellarg($this->temp) . ' 2>&1 &&echo $?')) !== '0' || !file_exists($this->temp)) + if ($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' . + sprintf($config['convert_args'], + $this->width, + $this->height, + escapeshellarg($this->src . '[0]'), + $this->width, + $this->height, + escapeshellarg($this->temp))) || !file_exists($this->temp)) error('Failed to resize image!', null, $error); } } -- cgit v1.3 From b67fc7d54e3fbde2f9e117a961998c5f8440679a Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 3 Aug 2013 20:50:37 -0400 Subject: Only use `identify` if we have to --- inc/image.php | 15 ++++++++++++--- post.php | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'inc/image.php') diff --git a/inc/image.php b/inc/image.php index 1dac4724..92e74277 100644 --- a/inc/image.php +++ b/inc/image.php @@ -11,7 +11,7 @@ if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) { class Image { public $src, $format, $image, $size; - public function __construct($src, $format = false) { + public function __construct($src, $format = false, $size = false) { global $config; $this->src = $src; @@ -28,7 +28,7 @@ class Image { } } - $this->image = new $classname($this); + $this->image = new $classname($this, $size); if (!$this->image->valid()) { $this->delete(); @@ -122,10 +122,15 @@ class ImageBase extends ImageGD { return (bool)$this->image; } - public function __construct($img) { + public function __construct($img, $size = false) { if (method_exists($this, 'init')) $this->init(); + if ($size && $size[0] > 0 && $size[1] > 0) { + $this->width = $size[0]; + $this->height = $size[1]; + } + if ($img !== false) { $this->src = $img->src; $this->from(); @@ -244,6 +249,10 @@ class ImageConvert extends ImageBase { $this->temp = false; } public function from() { + if ($this->width > 0 && $this->height > 0) { + $this->image = true; + return; + } $size = shell_exec_error(($this->gm ? 'gm ' : '') . 'identify -format "%w %h" ' . escapeshellarg($this->src . '[0]')); if (preg_match('/^(\d+) (\d+)$/', $size, $m)) { $this->width = $m[1]; diff --git a/post.php b/post.php index 905110d4..434bc7e2 100644 --- a/post.php +++ b/post.php @@ -464,7 +464,7 @@ if (isset($_POST['delete'])) { } // create image object - $image = new Image($upload, $post['extension']); + $image = new Image($upload, $post['extension'], $size); if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) { $image->delete(); error($config['error']['maxsize']); -- cgit v1.3