X-Git-Url: http://git.shiar.net/minimedit.git/blobdiff_plain/9a24b86c2c7ddd574bf21ad86877c9c3bf0f229e..a7c5afe4339f46523620adf4ca4368efe2330ea8:/thumb/index.php diff --git a/thumb/index.php b/thumb/index.php index 1267919..1d36948 100644 --- a/thumb/index.php +++ b/thumb/index.php @@ -13,11 +13,10 @@ if (!file_exists($imgpath)) { } } -$target = "thumb/$height/$imgpath"; try { - mkthumb($imgpath, $target, $width, $height); + $target = mkthumb($imgpath, $width, $height); } -catch (Exception $e) { +catch (Throwable $e) { http_response_code($e->getCode() ?: 500); $target = '500.png'; if (file_exists($target)) { @@ -34,14 +33,19 @@ header('Content-type: '.mime_content_type($target)); readfile($target); exit; -function mkthumb($source, $target, $width, $height) +function mkthumb($source, $width, $height) { + $target = "thumb/$height/$source"; + if (isset($_GET['backend'])) { $backend = $_GET['backend']; } elseif (file_exists($target)) { return; } + elseif (extension_loaded('gd')) { + $backend = 'gd'; + } else { $backend = 'exec'; } @@ -49,6 +53,21 @@ function mkthumb($source, $target, $width, $height) @mkdir(dirname($target), 0777, TRUE); $backend($source, $target, $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)