4 list ($height, $imgpath) = explode('/', ltrim($Args, '/'), 2);
6 $imgpath = preg_replace('{^(?=[0-9]+/)}', 'data/', $imgpath, 1);
8 if (!file_exists($imgpath)) {
9 http_response_code(404);
11 if (!file_exists($imgpath)) {
17 $target = mkthumb($imgpath, $width, $height);
19 catch (Throwable $e) {
20 http_response_code($e->getCode() ?: 500);
22 if (file_exists($target)) {
23 header("X-Error: ".$e->getMessage());
24 header('Content-type: '.mime_content_type($target));
28 trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING);
32 header('Content-type: '.mime_content_type($target));
36 function mkthumb($source, $width, $height)
38 $target = "thumb/$height/$source";
40 if (isset($_GET['backend'])) {
41 $backend = $_GET['backend'];
43 elseif (file_exists($target)) {
46 elseif (extension_loaded('gd')) {
52 $backend = "mkthumb_$backend";
54 @mkdir(dirname($target), 0777, TRUE);
55 $backend($source, $target, $width, $height);
59 function mkthumb_gd($source, $target, $width, $height)
61 $data = imagecreatefromstring(file_get_contents($source));
62 if (!$data) throw new Exception("error reading $source");
63 $orgwidth = imagesx($data);
64 $orgheight = imagesy($data);
65 $width = min($width, $orgwidth * $height / $orgheight);
66 $gd = imagecreatetruecolor($width, $height);
68 imagecopyresampled($gd, $data, 0, 0, 0, 0,
69 $width, $height, $orgwidth, $orgheight);
70 imagejpeg($gd, $target, 90);
73 function mkthumb_exec($source, $target, $width, $height)
75 if (!function_exists('popen')) {
76 throw new Exception("exec disallowed on this server", 501);
78 $cmd = implode(' ', array_map('escapeshellarg', [
81 '-resize', "${width}x${height}",
85 $return = shell_exec("$cmd 2>&1");
87 throw new Exception($return);