7de4f456939152bd931f61a15094b4a061ebbe08
[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 readfile($target);
37 exit;
38
39 function mkthumb($source, $size)
40 {
41         if (strpos($size, 'x') !== FALSE) {
42                 list ($width, $height) = explode('x', $size);
43                 if (empty($height)) {
44                         $height = $width * 4;
45                 }
46         }
47         else {
48                 $height = $size;
49         }
50         if (empty($width)) {
51                 $width = $height * 4;
52         }
53         $target = "thumb/$size/$source";
54
55         if (isset($_GET['backend'])) {
56                 $backend = $_GET['backend'];
57         }
58         elseif (file_exists($target)) {
59                 return $target;
60         }
61         elseif (extension_loaded('gd')) {
62                 $backend = 'gd';
63         }
64         else {
65                 $backend = 'exec';
66         }
67         $backend = "mkthumb_$backend";
68
69         @mkdir(dirname($target), 0777, TRUE);
70         $backend($source, $target, $width, $height);
71         return $target;
72 }
73
74 function mkthumb_gd($source, $target, $width, $height)
75 {
76         $data = imagecreatefromstring(file_get_contents($source));
77         if (!$data) throw new Exception("error reading $source");
78         $orgwidth = imagesx($data);
79         $orgheight = imagesy($data);
80         $width = min($width, $orgwidth * $height / $orgheight);
81         $gd = imagecreatetruecolor($width, $height);
82         //TODO: trim
83         imagecopyresampled($gd, $data, 0, 0, 0, 0,
84                         $width, $height, $orgwidth, $orgheight);
85         imagejpeg($gd, $target, 90);
86 }
87
88 function mkthumb_exec($source, $target, $width, $height)
89 {
90         if (!function_exists('popen')) {
91                 throw new Exception("exec disallowed on this server", 501);
92         }
93         $cmd = implode(' ', array_map('escapeshellarg', [
94                 'convert',
95                 '-delete', '1--1', # static
96                 '-trim',
97                 '-background', 'white', '-layers', 'flatten', # opaque
98                 '-auto-orient', # apply exif rotation
99                 '-interlace', 'plane', # progressive
100                 '-strip', '-taint', # omit metadata
101                 '-sampling-factor', '4:2:0', '-colorspace', 'sRGB', # half chroma
102                 '-resize', "${width}x${height}",
103                 '-quality', '85%',
104                 $source, "jpg:$target"
105         ]));
106         $return = shell_exec("$cmd 2>&1");
107         if ($return) {
108                 throw new Exception($return);
109         }
110 }