thumb: prefer native GD over external convert
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 19 Jun 2018 22:17:48 +0000 (00:17 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 7 Jul 2018 11:42:39 +0000 (13:42 +0200)
Commonly available unlike executed command which may be disallowed
(at Xenat) or missing (at Vimexx) on shared hosting servers.

thumb/index.php

index 2849bb50d59f689bfdc927a34ae0b1eb04938f34..532499f6aa2f14f3cd2f4aea9dbd7481ae9feea5 100644 (file)
@@ -43,6 +43,9 @@ function mkthumb($source, $width, $height)
        elseif (file_exists($target)) {
                return;
        }
+       elseif (extension_loaded('gd')) {
+               $backend = 'gd';
+       }
        else {
                $backend = 'exec';
        }
@@ -53,6 +56,20 @@ function mkthumb($source, $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)
 {
        if (!function_exists('popen')) {