5327192d0be49e85632679f256cacb5fa91b2306
[minimedit.git] / thumb / index.php
1 <?php
2 ob_clean();
3
4 list ($height, $imgpath) = explode('/', ltrim($Args, '/'), 2);
5 $width= 1000;
6 $imgpath = preg_replace('{^(?=[0-9]+/)}', 'data/', $imgpath, 1);
7
8 if (!file_exists($imgpath)) {
9         http_response_code(404);
10         exit;
11 }
12
13 $target = "thumb/$height/$imgpath";
14 if (!file_exists($target)) {
15         @mkdir(dirname($target), 0777, TRUE);
16
17         $cmd = implode(' ', array_map('escapeshellarg', [
18                 'convert',
19                 '-trim',
20                 '-resize', "${width}x${height}",
21                 '-quality', '90%',
22                 $imgpath, $target
23         ]));
24         $return = shell_exec("$cmd 2>&1");
25         if ($return) {
26                 http_response_code(500);
27                 trigger_error("thumbnail creation failed: $return", E_USER_WARNING);
28                 exit;
29         }
30 }
31
32 header('Content-type: '.mime_content_type($target));
33 readfile($target);
34 exit;