thumb: flatten transparent images to white background
[minimedit.git] / thumb / index.php
index 066e88d7cf9561227d96f8b6a2fe9ff80afacc21..f15a180b1e8e89983eea66d5430f7ee7fc19e159 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 ob_clean();
 
-list ($height, $imgpath) = explode('/', ltrim($Args, '/'), 2);
-$width= 1000;
+list ($size, $imgpath) = explode('/', ltrim($Args, '/'), 2);
 $imgpath = preg_replace('{^(?=[0-9]+/)}', 'data/', $imgpath, 1);
 
 if (!file_exists($imgpath)) {
@@ -13,34 +12,51 @@ if (!file_exists($imgpath)) {
        }
 }
 
-$target = "thumb/$height/$imgpath";
-if (!file_exists($target)) {
-       try {
-               mkthumb($imgpath, $target, $width, $height);
-       }
-       catch (Exception $e) {
-               http_response_code($e->getCode() ?: 500);
-               $target = '500.png';
-               if (file_exists($target)) {
-                       header("X-Error: ".$e->getMessage());
-                       header('Content-type: '.mime_content_type($target));
-                       readfile($target);
-                       exit;
-               }
-               trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING);
+try {
+       $target = mkthumb($imgpath, $size);
+}
+catch (Throwable $e) {
+       http_response_code($e->getCode() ?: 500);
+       header("X-Error: ".explode("\n", $e->getMessage())[0], FALSE);
+       $target = '500.png';
+       if (file_exists($target)) {
+               header('Content-type: '.mime_content_type($target));
+               readfile($target);
                exit;
        }
+       trigger_error("thumbnail creation failed: ".$e->getMessage(), E_USER_WARNING);
+       exit;
 }
 
 header('Content-type: '.mime_content_type($target));
 readfile($target);
 exit;
 
-function mkthumb($source, $target, $width, $height)
+function mkthumb($source, $size)
 {
+       if (strpos($size, 'x') !== FALSE) {
+               list ($width, $height) = explode('x', $size);
+               if (empty($height)) {
+                       $height = $width * 4;
+               }
+       }
+       else {
+               $height = $size;
+       }
+       if (empty($width)) {
+               $width = $height * 4;
+       }
+       $target = "thumb/$size/$source";
+
        if (isset($_GET['backend'])) {
                $backend = $_GET['backend'];
        }
+       elseif (file_exists($target)) {
+               return $target;
+       }
+       elseif (extension_loaded('gd')) {
+               $backend = 'gd';
+       }
        else {
                $backend = 'exec';
        }
@@ -48,6 +64,21 @@ function mkthumb($source, $target, $width, $height)
 
        @mkdir(dirname($target), 0777, TRUE);
        $backend($source, $target, $width, $height);
+       return $target;
+}
+
+function mkthumb_gd($source, $target, $width, $height)
+{
+       $data = imagecreatefromstring(file_get_contents($source));
+       if (!$data) throw new Exception("error reading $source");
+       $orgwidth = imagesx($data);
+       $orgheight = imagesy($data);
+       $width = min($width, $orgwidth * $height / $orgheight);
+       $gd = imagecreatetruecolor($width, $height);
+       //TODO: trim
+       imagecopyresampled($gd, $data, 0, 0, 0, 0,
+                       $width, $height, $orgwidth, $orgheight);
+       imagejpeg($gd, $target, 90);
 }
 
 function mkthumb_exec($source, $target, $width, $height)
@@ -58,9 +89,10 @@ function mkthumb_exec($source, $target, $width, $height)
        $cmd = implode(' ', array_map('escapeshellarg', [
                'convert',
                '-trim',
+               '-background', 'white', '-layers', 'flatten',
                '-resize', "${width}x${height}",
                '-quality', '90%',
-               $source, $target
+               $source, "jpg:$target"
        ]));
        $return = shell_exec("$cmd 2>&1");
        if ($return) {