thumb: indicate 1 month caching of generated output
[minimedit.git] / thumb / index.php
1 <?php
2 ob_clean();
3
4 list ($size, $imgpath) = explode('/', ltrim($Args, '/'), 2);
5 $imgpath = preg_replace('{^(?=[0-9]+/)}', 'data/', $imgpath, 1);
6
7 if (!file_exists($imgpath)) {
8         http_response_code(404);
9         $imgpath = '404.png';
10         if (!file_exists($imgpath)) {
11                 exit;
12         }
13 }
14
15 try {
16         $target = mkthumb($imgpath, $size);
17 }
18 catch (Throwable $e) {
19         http_response_code($e->getCode() ?: 500);
20         header("X-Error: ".explode("\n", $e->getMessage())[0], FALSE);
21         $target = '500.png';
22         if (file_exists($target)) {
23                 header('Content-type: '.mime_content_type($target));
24                 readfile($target);
25                 exit;
26         }
27         trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING);
28         exit;
29 }
30
31 header('Cache-Control: max-age=2628000, immutable');
32 header('Content-type: '.mime_content_type($target));
33 readfile($target);
34 exit;
35
36 function mkthumb($source, $size)
37 {
38         if (strpos($size, 'x') !== FALSE) {
39                 list ($width, $height) = explode('x', $size);
40                 if (empty($height)) {
41                         $height = $width * 4;
42                 }
43         }
44         else {
45                 $height = $size;
46         }
47         if (empty($width)) {
48                 $width = $height * 4;
49         }
50         $target = "thumb/$size/$source";
51
52         if (isset($_GET['backend'])) {
53                 $backend = $_GET['backend'];
54         }
55         elseif (file_exists($target)) {
56                 return $target;
57         }
58         elseif (extension_loaded('gd')) {
59                 $backend = 'gd';
60         }
61         else {
62                 $backend = 'exec';
63         }
64         $backend = "mkthumb_$backend";
65
66         @mkdir(dirname($target), 0777, TRUE);
67         $backend($source, $target, $width, $height);
68         return $target;
69 }
70
71 function mkthumb_gd($source, $target, $width, $height)
72 {
73         $data = imagecreatefromstring(file_get_contents($source));
74         if (!$data) throw new Exception("error reading $source");
75         $orgwidth = imagesx($data);
76         $orgheight = imagesy($data);
77         $width = min($width, $orgwidth * $height / $orgheight);
78         $gd = imagecreatetruecolor($width, $height);
79         //TODO: trim
80         imagecopyresampled($gd, $data, 0, 0, 0, 0,
81                         $width, $height, $orgwidth, $orgheight);
82         imagejpeg($gd, $target, 90);
83 }
84
85 function mkthumb_exec($source, $target, $width, $height)
86 {
87         if (!function_exists('popen')) {
88                 throw new Exception("exec disallowed on this server", 501);
89         }
90         $cmd = implode(' ', array_map('escapeshellarg', [
91                 'convert',
92                 '-trim',
93                 '-background', 'white', '-layers', 'flatten',
94                 '-resize', "${width}x${height}",
95                 '-quality', '90%',
96                 $source, "jpg:$target"
97         ]));
98         $return = shell_exec("$cmd 2>&1");
99         if ($return) {
100                 throw new Exception($return);
101         }
102 }