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