Browse Source

Support for resizing gifs using gifsicle with resizing the rest using ImageMagick

pull/40/head
Marcin Łabanowski 12 years ago
committed by Michael Foster
parent
commit
b7e725bf3e
  1. 15
      inc/config.php
  2. 28
      inc/image.php

15
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

28
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))

Loading…
Cancel
Save