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