thumb: dynamic image relay with resize option
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Oct 2017 20:03:55 +0000 (22:03 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 16 Jun 2018 19:52:51 +0000 (21:52 +0200)
Generate image with requested height from data source, which should be
bypassed by the server if target file exists.

Copied from Lijtweg at commit v2.5-58-gac5434f0af (2017-12-12).

thumb/.gitignore [new file with mode: 0644]
thumb/.htaccess [new file with mode: 0644]
thumb/index.php [new file with mode: 0644]

diff --git a/thumb/.gitignore b/thumb/.gitignore
new file mode 100644 (file)
index 0000000..34bbc9f
--- /dev/null
@@ -0,0 +1 @@
+/[1-9]*/
diff --git a/thumb/.htaccess b/thumb/.htaccess
new file mode 100644 (file)
index 0000000..c63c79e
--- /dev/null
@@ -0,0 +1,3 @@
+<IfModule mod_headers.c>
+       Header set Cache-Control "max-age=2628000, immutable"
+</IfModule>
diff --git a/thumb/index.php b/thumb/index.php
new file mode 100644 (file)
index 0000000..b49ca6e
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+ob_clean();
+
+list ($height, $imgarg) = explode('/', ltrim($Args, '/'), 2);
+$width= 1000;
+$imgpath = "data/$imgarg";
+
+if (!file_exists($imgpath)) {
+       http_response_code(404);
+       exit;
+}
+
+$target = preg_replace("{^data/}", "thumb/$height/", $imgpath, 1);
+if (!file_exists($target)) {
+       @mkdir(dirname($target), 0777, TRUE);
+
+       $cmd = implode(' ', array_map('escapeshellarg', [
+               'convert',
+               '-trim',
+               '-resize', "${width}x${height}",
+               '-quality', '90%',
+               $imgpath, $target
+       ]));
+       $return = shell_exec("$cmd 2>&1");
+       if ($return) {
+               http_response_code(500);
+               trigger_error("thumbnail creation failed: $return", E_USER_WARNING);
+               exit;
+       }
+}
+
+header('Content-type: '.mime_content_type($target));
+readfile($target);
+exit;