thumb: return 501 image if resize is unavailable
[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 (!function_exists('popen')) {
9         http_response_code(501);
10         $target = '501.png';
11         header('Content-type: '.mime_content_type($target));
12         readfile($target);
13         exit;
14 }
15
16 if (!file_exists($imgpath)) {
17         http_response_code(404);
18         exit;
19 }
20
21 $target = "thumb/$height/$imgpath";
22 if (!file_exists($target)) {
23         @mkdir(dirname($target), 0777, TRUE);
24
25         $cmd = implode(' ', array_map('escapeshellarg', [
26                 'convert',
27                 '-trim',
28                 '-resize', "${width}x${height}",
29                 '-quality', '90%',
30                 $imgpath, $target
31         ]));
32         $return = shell_exec("$cmd 2>&1");
33         if ($return) {
34                 http_response_code(500);
35                 trigger_error("thumbnail creation failed: $return", E_USER_WARNING);
36                 exit;
37         }
38 }
39
40 header('Content-type: '.mime_content_type($target));
41 readfile($target);
42 exit;