Add ImageMagic support to thumbnail process

This commit is contained in:
Trevor Slocum 2015-06-25 00:49:25 -07:00
parent 9b41846002
commit 5c0f55681c
4 changed files with 65 additions and 46 deletions

View File

@ -48,6 +48,10 @@ Installing
- Set ``TINYIB_REQMOD`` to ``all`` to require moderation for all posts. - Set ``TINYIB_REQMOD`` to ``all`` to require moderation for all posts.
- Moderate posts by visiting the management panel. - 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. - 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: 6. [CHMOD](http://en.wikipedia.org/wiki/Chmod) write permissions to these directories:
- ./ (the directory containing TinyIB) - ./ (the directory containing TinyIB)
- ./src/ - ./src/

View File

@ -27,6 +27,9 @@ if (!defined('TINYIB_SWF')) {
if (!defined('TINYIB_WEBM')) { if (!defined('TINYIB_WEBM')) {
define('TINYIB_WEBM', false); define('TINYIB_WEBM', false);
} }
if (!defined('TINYIB_THUMBNAIL')) {
define('TINYIB_THUMBNAIL', 'gd');
}
if (!defined('TINYIB_NOFILEOK')) { if (!defined('TINYIB_NOFILEOK')) {
define('TINYIB_NOFILEOK', false); define('TINYIB_NOFILEOK', false);
} }

View File

@ -361,15 +361,16 @@ 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']); 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) { function createThumbnail($file_location, $thumb_location, $new_w, $new_h) {
$system = explode(".", $filename); if (TINYIB_THUMBNAIL == 'gd') {
$system = explode(".", $thumb_location);
$system = array_reverse($system); $system = array_reverse($system);
if (preg_match("/jpg|jpeg/", $system[0])) { if (preg_match("/jpg|jpeg/", $system[0])) {
$src_img = imagecreatefromjpeg($name); $src_img = imagecreatefromjpeg($file_location);
} else if (preg_match("/png/", $system[0])) { } else if (preg_match("/png/", $system[0])) {
$src_img = imagecreatefrompng($name); $src_img = imagecreatefrompng($file_location);
} else if (preg_match("/gif/", $system[0])) { } else if (preg_match("/gif/", $system[0])) {
$src_img = imagecreatefromgif($name); $src_img = imagecreatefromgif($file_location);
} else { } else {
return false; return false;
} }
@ -377,6 +378,7 @@ function createThumbnail($name, $filename, $new_w, $new_h) {
if (!$src_img) { 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."); 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_x = imageSX($src_img);
$old_y = imageSY($src_img); $old_y = imageSY($src_img);
$percent = ($old_x > $old_y) ? ($new_w / $old_x) : ($new_h / $old_y); $percent = ($old_x > $old_y) ? ($new_w / $old_x) : ($new_h / $old_y);
@ -384,7 +386,7 @@ function createThumbnail($name, $filename, $new_w, $new_h) {
$thumb_h = round($old_y * $percent); $thumb_h = round($old_y * $percent);
$dst_img = imagecreatetruecolor($thumb_w, $thumb_h); $dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
if (preg_match("/png/", $system[0]) && imagepng($src_img, $filename)) { if (preg_match("/png/", $system[0]) && imagepng($src_img, $thumb_location)) {
imagealphablending($dst_img, false); imagealphablending($dst_img, false);
imagesavealpha($dst_img, true); imagesavealpha($dst_img, true);
@ -398,21 +400,30 @@ function createThumbnail($name, $filename, $new_w, $new_h) {
} }
if (preg_match("/png/", $system[0])) { if (preg_match("/png/", $system[0])) {
if (!imagepng($dst_img, $filename)) { if (!imagepng($dst_img, $thumb_location)) {
return false; return false;
} }
} else if (preg_match("/jpg|jpeg/", $system[0])) { } else if (preg_match("/jpg|jpeg/", $system[0])) {
if (!imagejpeg($dst_img, $filename, 70)) { if (!imagejpeg($dst_img, $thumb_location, 70)) {
return false; return false;
} }
} else if (preg_match("/gif/", $system[0])) { } else if (preg_match("/gif/", $system[0])) {
if (!imagegif($dst_img, $filename)) { if (!imagegif($dst_img, $thumb_location)) {
return false; return false;
} }
} }
imagedestroy($dst_img); imagedestroy($dst_img);
imagedestroy($src_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;
}
}
return true; return true;
} }

View File

@ -37,6 +37,7 @@ define('TINYIB_WEBM', false); // Enable .weba and .webm audio/video file
// File control // File control
define('TINYIB_MAXKB', 2048); // Maximum file size in kilobytes [0 to disable] 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_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 define('TINYIB_NOFILEOK', false); // Allow the creation of new threads without uploading a file
// Thumbnail size - new thread // Thumbnail size - new thread