From b7e725bf3e0394193dee9373c18dec7f727ea6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=81abanowski?= Date: Mon, 24 Dec 2012 05:34:06 +0100 Subject: [PATCH] Support for resizing gifs using gifsicle with resizing the rest using ImageMagick --- inc/config.php | 15 ++++++++++----- inc/image.php | 28 ++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/inc/config.php b/inc/config.php index 36518028..863b9043 100644 --- a/inc/config.php +++ b/inc/config.php @@ -413,11 +413,16 @@ $config['thumb_keep_animation_frames'] = 1; // Thumbnailing method: - // - 'gd' PHP GD (default). Only handles the most basic image formats (GIF, JPEG, PNG). This is a prerequisite - // for Tinyboard no matter what method you choose. - // - 'imagick' PHP's ImageMagick bindings. Fast and efficient, supporting many image formats. A few minor bugs. - // http://pecl.php.net/package/imagick - // - 'convert' The command line version of ImageMagick (`convert`). Fixes most of the bugs in PHP Imagick. + // - 'gd' PHP GD (default). Only handles the most basic image formats (GIF, JPEG, PNG). + // This is a prerequisite for Tinyboard no matter what method you choose. + // - 'imagick' PHP's ImageMagick bindings. Fast and efficient, supporting many image formats. + // A few minor bugs. http://pecl.php.net/package/imagick + // - 'convert' The command line version of ImageMagick (`convert`). Fixes most of the bugs in + // PHP Imagick. + // - 'convert+gifsicle' Same as above, with the exception of using `gifsicle` (command line application) + // instead of `convert` for resizing gifs. It's faster and resulting animated gifs + // have less artifacts than if resized with ImageMagick. + $config['thumb_method'] = 'gd'; // Strip EXIF metadata from JPEG files diff --git a/inc/image.php b/inc/image.php index f0909b8c..e4738953 100644 --- a/inc/image.php +++ b/inc/image.php @@ -16,10 +16,10 @@ class Image { $this->src = $src; $this->format = $format; - + if ($config['thumb_method'] == 'imagick') { $classname = 'ImageImagick'; - } elseif ($config['thumb_method'] == 'convert') { + } elseif ($config['thumb_method'] == 'convert' || $config['thumb_method'] == 'convert+gifsicle') { $classname = 'ImageConvert'; } else { $classname = 'Image' . strtoupper($this->format); @@ -29,6 +29,7 @@ class Image { } $this->image = new $classname($this); + if (!$this->image->valid()) { $this->delete(); error($config['error']['invalidimg']); @@ -44,10 +45,15 @@ class Image { public function resize($extension, $max_width, $max_height) { global $config; + $gifsicle = false; + if ($config['thumb_method'] == 'imagick') { $classname = 'ImageImagick'; } elseif ($config['thumb_method'] == 'convert') { $classname = 'ImageConvert'; + } elseif ($config['thumb_method'] == 'convert+gifsicle') { + $classname = 'ImageConvert'; + $gifsicle = true; } else { $classname = 'Image' . strtoupper($extension); if (!class_exists($classname)) { @@ -75,6 +81,9 @@ class Image { $height = $max_height; } + if ($gifsicle) { + $thumb->gifsicle = 1; + } $thumb->_resize($this->image->image, $width, $height); return $thumb; @@ -219,7 +228,7 @@ class ImageImagick extends ImageBase { class ImageConvert extends ImageBase { - public $width, $height, $temp; + public $width, $height, $temp, $gifsicle; public function init() { global $config; @@ -275,9 +284,16 @@ class ImageConvert extends ImageBase { $quality = $config['thumb_quality'] * 10; if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '') && $config['thumb_keep_animation_frames'] > 1) { - if (shell_exec("convert -background transparent -filter Point -sample {$this->width}x{$this->height} +antialias -quality {$quality} " . - escapeshellarg($this->src . '') . " " . escapeshellarg($this->temp)) || !file_exists($this->temp)) - error('Failed to resize image!'); + if ($this->gifsicle) { + if (shell_exec("gifsicle --unoptimize -O2 --resize {$this->width}x{$this->height} " . + escapeshellarg($this->src . '') . " " . escapeshellarg($this->temp)) || !file_exists($this->temp)) + error('Failed to resize image!'); + } + else { + if (shell_exec("convert -background transparent -filter Point -sample {$this->width}x{$this->height} +antialias -quality {$quality} " . + escapeshellarg($this->src . '') . " " . escapeshellarg($this->temp)) || !file_exists($this->temp)) + error('Failed to resize image!'); + } } else { if (shell_exec("convert -background transparent -flatten -filter Point -scale {$this->width}x{$this->height} +antialias -quality {$quality} " . escapeshellarg($this->src . '[0]') . " " . escapeshellarg($this->temp)) || !file_exists($this->temp))