From 5c0f55681c7844d6ba783c836e03b5d642302b0b Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Thu, 25 Jun 2015 00:49:25 -0700 Subject: [PATCH] Add ImageMagic support to thumbnail process --- README.md | 4 ++ inc/defines.php | 3 ++ inc/functions.php | 103 ++++++++++++++++++++++++------------------- settings.default.php | 1 + 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 96c1756..c86548b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,10 @@ Installing - Set ``TINYIB_REQMOD`` to ``all`` to require moderation for all posts. - Moderate posts by visiting the management panel. - When setting ``TINYIB_DBMODE`` to ``pdo``, note that PDO mode has been tested on **MySQL databases only**. Theoretically it will work with any applicable driver, but this is not guaranteed. If you use an alternative driver, please report back regarding how it works. + - To use ImageMagick instead of GD when creating thumbnails: + - Install ImageMagick and ensure that the ``convert`` command is available. + - Set ``TINYIB_THUMBNAIL`` to ``imagemagick``. + - **Note:** GIF files will have animated thumbnails, which will often have large file sizes. 6. [CHMOD](http://en.wikipedia.org/wiki/Chmod) write permissions to these directories: - ./ (the directory containing TinyIB) - ./src/ diff --git a/inc/defines.php b/inc/defines.php index d1392ea..dd966f0 100644 --- a/inc/defines.php +++ b/inc/defines.php @@ -27,6 +27,9 @@ if (!defined('TINYIB_SWF')) { if (!defined('TINYIB_WEBM')) { define('TINYIB_WEBM', false); } +if (!defined('TINYIB_THUMBNAIL')) { + define('TINYIB_THUMBNAIL', 'gd'); +} if (!defined('TINYIB_NOFILEOK')) { define('TINYIB_NOFILEOK', false); } diff --git a/inc/functions.php b/inc/functions.php index 25e6937..8541b62 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -361,59 +361,70 @@ function thumbnailDimensions($post) { return ($post['image_width'] > $max_width || $post['image_height'] > $max_height) ? array($max_width, $max_height) : array($post['image_width'], $post['image_height']); } -function createThumbnail($name, $filename, $new_w, $new_h) { - $system = explode(".", $filename); - $system = array_reverse($system); - if (preg_match("/jpg|jpeg/", $system[0])) { - $src_img = imagecreatefromjpeg($name); - } else if (preg_match("/png/", $system[0])) { - $src_img = imagecreatefrompng($name); - } else if (preg_match("/gif/", $system[0])) { - $src_img = imagecreatefromgif($name); - } else { - return false; - } - - if (!$src_img) { - fancyDie("Unable to read uploaded file during thumbnailing. A common cause for this is an incorrect extension when the file is actually of a different type."); - } - $old_x = imageSX($src_img); - $old_y = imageSY($src_img); - $percent = ($old_x > $old_y) ? ($new_w / $old_x) : ($new_h / $old_y); - $thumb_w = round($old_x * $percent); - $thumb_h = round($old_y * $percent); - - $dst_img = imagecreatetruecolor($thumb_w, $thumb_h); - if (preg_match("/png/", $system[0]) && imagepng($src_img, $filename)) { - imagealphablending($dst_img, false); - imagesavealpha($dst_img, true); - - $color = imagecolorallocatealpha($dst_img, 0, 0, 0, 0); - imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $color); - imagecolortransparent($dst_img, $color); - - imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); - } else { - fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); - } - - if (preg_match("/png/", $system[0])) { - if (!imagepng($dst_img, $filename)) { +function createThumbnail($file_location, $thumb_location, $new_w, $new_h) { + if (TINYIB_THUMBNAIL == 'gd') { + $system = explode(".", $thumb_location); + $system = array_reverse($system); + if (preg_match("/jpg|jpeg/", $system[0])) { + $src_img = imagecreatefromjpeg($file_location); + } else if (preg_match("/png/", $system[0])) { + $src_img = imagecreatefrompng($file_location); + } else if (preg_match("/gif/", $system[0])) { + $src_img = imagecreatefromgif($file_location); + } else { return false; } - } else if (preg_match("/jpg|jpeg/", $system[0])) { - if (!imagejpeg($dst_img, $filename, 70)) { - return false; + + if (!$src_img) { + fancyDie("Unable to read uploaded file during thumbnailing. A common cause for this is an incorrect extension when the file is actually of a different type."); } - } else if (preg_match("/gif/", $system[0])) { - if (!imagegif($dst_img, $filename)) { + + $old_x = imageSX($src_img); + $old_y = imageSY($src_img); + $percent = ($old_x > $old_y) ? ($new_w / $old_x) : ($new_h / $old_y); + $thumb_w = round($old_x * $percent); + $thumb_h = round($old_y * $percent); + + $dst_img = imagecreatetruecolor($thumb_w, $thumb_h); + if (preg_match("/png/", $system[0]) && imagepng($src_img, $thumb_location)) { + imagealphablending($dst_img, false); + imagesavealpha($dst_img, true); + + $color = imagecolorallocatealpha($dst_img, 0, 0, 0, 0); + imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $color); + imagecolortransparent($dst_img, $color); + + imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); + } else { + fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); + } + + if (preg_match("/png/", $system[0])) { + if (!imagepng($dst_img, $thumb_location)) { + return false; + } + } else if (preg_match("/jpg|jpeg/", $system[0])) { + if (!imagejpeg($dst_img, $thumb_location, 70)) { + return false; + } + } else if (preg_match("/gif/", $system[0])) { + if (!imagegif($dst_img, $thumb_location)) { + return false; + } + } + + imagedestroy($dst_img); + imagedestroy($src_img); + } else { // imagemagick + $discard = ''; + $exit_status = 1; + exec("convert $file_location -auto-orient -thumbnail '" . $new_w . "x" . $new_h . "' -coalesce -layers OptimizeFrame -depth 4 -type palettealpha $thumb_location", $discard, $exit_status); + + if ($exit_status != 0) { return false; } } - imagedestroy($dst_img); - imagedestroy($src_img); - return true; } diff --git a/settings.default.php b/settings.default.php index 12bc1f4..fa4b6a8 100644 --- a/settings.default.php +++ b/settings.default.php @@ -37,6 +37,7 @@ define('TINYIB_WEBM', false); // Enable .weba and .webm audio/video file // File control define('TINYIB_MAXKB', 2048); // Maximum file size in kilobytes [0 to disable] define('TINYIB_MAXKBDESC', "2 MB"); // Human-readable representation of the maximum file size +define('TINYIB_THUMBNAIL', 'gd'); // Thumbnail method to use: gd / imagemagick (see README for instructions) define('TINYIB_NOFILEOK', false); // Allow the creation of new threads without uploading a file // Thumbnail size - new thread