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