From e5da0ad603c58c2dd86581e35d6e1a60c0ae2e7d Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Wed, 20 Jun 2018 00:17:48 +0200 Subject: [PATCH] thumb: prefer native GD over external convert Commonly available unlike executed command which may be disallowed (at Xenat) or missing (at Vimexx) on shared hosting servers. --- thumb/index.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/thumb/index.php b/thumb/index.php index 2849bb5..532499f 100644 --- a/thumb/index.php +++ b/thumb/index.php @@ -43,6 +43,9 @@ function mkthumb($source, $width, $height) elseif (file_exists($target)) { return; } + elseif (extension_loaded('gd')) { + $backend = 'gd'; + } else { $backend = 'exec'; } @@ -53,6 +56,20 @@ function mkthumb($source, $width, $height) return $target; } +function mkthumb_gd($source, $target, $width, $height) +{ + $data = imagecreatefromstring(file_get_contents($source)); + if (!$data) throw new Exception("error reading $source"); + $orgwidth = imagesx($data); + $orgheight = imagesy($data); + $width = min($width, $orgwidth * $height / $orgheight); + $gd = imagecreatetruecolor($width, $height); + //TODO: trim + imagecopyresampled($gd, $data, 0, 0, 0, 0, + $width, $height, $orgwidth, $orgheight); + imagejpeg($gd, $target, 90); +} + function mkthumb_exec($source, $target, $width, $height) { if (!function_exists('popen')) { -- 2.30.0