From 6abd5c6f3f81ff63032fe9d85a10b2b66acd7b14 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Wed, 4 Jul 2018 18:45:59 +0200 Subject: [PATCH] thumb: move imagemagick execution to separate function Code cleanup to prepare for different resizing backends. --- thumb/index.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/thumb/index.php b/thumb/index.php index 28f3b9e..66de106 100644 --- a/thumb/index.php +++ b/thumb/index.php @@ -20,23 +20,37 @@ if (!file_exists($imgpath)) { $target = "thumb/$height/$imgpath"; if (!file_exists($target)) { + try { + mkthumb($imgpath, $target, $width, $height); + } + catch (Exception $e) { + http_response_code(500); + trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING); + exit; + } +} + +header('Content-type: '.mime_content_type($target)); +readfile($target); +exit; + +function mkthumb($source, $target, $width, $height) +{ @mkdir(dirname($target), 0777, TRUE); + return mkthumb_exec($source, $target, $width, $height); +} +function mkthumb_exec($source, $target, $width, $height) +{ $cmd = implode(' ', array_map('escapeshellarg', [ 'convert', '-trim', '-resize', "${width}x${height}", '-quality', '90%', - $imgpath, $target + $source, $target ])); $return = shell_exec("$cmd 2>&1"); if ($return) { - http_response_code(500); - trigger_error("thumbnail creation failed: $return", E_USER_WARNING); - exit; + throw new Exception($return); } } - -header('Content-type: '.mime_content_type($target)); -readfile($target); -exit; -- 2.30.0