24f46ccb97ab44ea5125c48d63dc18a03283d4bd
[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         $target = '500.png';
21         if (file_exists($target)) {
22                 header("X-Error: ".$e->getMessage());
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('Content-type: '.mime_content_type($target));
32 readfile($target);
33 exit;
34
35 function mkthumb($source, $size)
36 {
37         if (strpos($size, 'x') !== FALSE) {
38                 list ($width, $height) = explode('x', $size);
39                 if (empty($height)) {
40                         $height = $width * 4;
41                 }
42         }
43         else {
44                 $height = $size;
45         }
46         if (empty($width)) {
47                 $width = $height * 4;
48         }
49         $target = "thumb/$size/$source";
50
51         if (isset($_GET['backend'])) {
52                 $backend = $_GET['backend'];
53         }
54         elseif (file_exists($target)) {
55                 return $target;
56         }
57         elseif (extension_loaded('gd')) {
58                 $backend = 'gd';
59         }
60         else {
61                 $backend = 'exec';
62         }
63         $backend = "mkthumb_$backend";
64
65         @mkdir(dirname($target), 0777, TRUE);
66         $backend($source, $target, $width, $height);
67         return $target;
68 }
69
70 function mkthumb_gd($source, $target, $width, $height)
71 {
72         $data = imagecreatefromstring(file_get_contents($source));
73         if (!$data) throw new Exception("error reading $source");
74         $orgwidth = imagesx($data);
75         $orgheight = imagesy($data);
76         $width = min($width, $orgwidth * $height / $orgheight);
77         $gd = imagecreatetruecolor($width, $height);
78         //TODO: trim
79         imagecopyresampled($gd, $data, 0, 0, 0, 0,
80                         $width, $height, $orgwidth, $orgheight);
81         imagejpeg($gd, $target, 90);
82 }
83
84 function mkthumb_exec($source, $target, $width, $height)
85 {
86         if (!function_exists('popen')) {
87                 throw new Exception("exec disallowed on this server", 501);
88         }
89         $cmd = implode(' ', array_map('escapeshellarg', [
90                 'convert',
91                 '-trim',
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 }