From: Mischa POSLAWSKY Date: Tue, 19 Jun 2018 22:17:48 +0000 (+0200) Subject: thumb: prefer native GD over external convert X-Git-Tag: v3.3~10 X-Git-Url: http://git.shiar.net/minimedit.git/commitdiff_plain/e5da0ad603c58c2dd86581e35d6e1a60c0ae2e7d 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. --- 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')) {