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);
13 $target = "thumb/$height/$imgpath";
14 if (!file_exists($target)) {
16 mkthumb($imgpath, $target, $width, $height);
18 catch (Exception $e) {
19 http_response_code($e->getCode() ?: 500);
21 if (file_exists($target)) {
22 header("X-Error: ".$e->getMessage());
23 header('Content-type: '.mime_content_type($target));
27 trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING);
32 header('Content-type: '.mime_content_type($target));
36 function mkthumb($source, $target, $width, $height)
38 @mkdir(dirname($target), 0777, TRUE);
39 return mkthumb_exec($source, $target, $width, $height);
42 function mkthumb_exec($source, $target, $width, $height)
44 if (!function_exists('popen')) {
45 throw new Exception("exec disallowed on this server", 501);
47 $cmd = implode(' ', array_map('escapeshellarg', [
50 '-resize', "${width}x${height}",
54 $return = shell_exec("$cmd 2>&1");
56 throw new Exception($return);