]> _ Git - cubist_util.git/commitdiff
wip #5408 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 14:19:07 +0000 (16:19 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 14:19:07 +0000 (16:19 +0200)
src/Graphics/Resizer.php [new file with mode: 0644]

diff --git a/src/Graphics/Resizer.php b/src/Graphics/Resizer.php
new file mode 100644 (file)
index 0000000..44981bd
--- /dev/null
@@ -0,0 +1,183 @@
+<?php
+
+namespace Cubist\Util\Graphics;
+
+use Cubist\Util\CommandLine;
+use Cubist\Util\Files\Files;
+
+class Resizer
+{
+    protected $im;
+    protected $dim;
+    protected $_exec;
+    protected $_acceptExts = array('png', 'jpg', 'jpeg', 'pdf', 'tif', 'tiff', 'gif', 'psd');
+    protected $_acceptWriteExts = array('png', 'jpg', 'jpeg', 'pdf', 'tif', 'tiff', 'gif', 'psd');
+    protected $_f = '';
+    protected $_background = '';
+    protected $_resize = '';
+    protected $_quality = '';
+
+    public function __construct($path = null)
+    {
+        $this->_exec = is_null($path) ? 'convert' : $path;
+    }
+
+    public function loadImage($f)
+    {
+        $this->_f = $f;
+        if ($this->_isMulti($this->_f)) {
+            $this->_f .= '[0]';
+        }
+    }
+
+    protected function _isMulti($f)
+    {
+        $ext = Files::getExtension($f);
+        return in_array($ext, array('pdf', 'tif', 'tiff'));
+    }
+
+    public function resizeRelative($ratio, $fillColor = null)
+    {
+        if ($fillColor) {
+            $this->_background = '-background ' . $this->_back($fillColor) . ' ';
+        }
+        $this->_resize .= '-resize ' . round($ratio * 100) . '%';
+    }
+
+    public function resize($WIDTH = null, $HEIGHT = null, $MODE = 'ratio', $EXPAND = false, $H = 'C', $V = 'M', $fillColor = null)
+    {
+        if ($fillColor) {
+            $this->_background = ' -background ' . $this->_back($fillColor);
+        }
+        $this->_resize = '';
+
+        if ($MODE == 'crop') {
+            if ($HEIGHT == -1) {
+                $HEIGHT = $WIDTH;
+            }
+
+            $this->_resize .= '-resize ' . $WIDTH . 'x' . $HEIGHT . '^ ';
+            $this->_resize .= '-gravity ' . $this->_getGravity($H, $V) . ' ';
+            $this->_resize .= '-extent ' . $WIDTH . 'x' . $HEIGHT . ' ';
+        } else if ($MODE == 'ratio') {
+            // If EXPAND is set to false, we need to pass an extra parameter
+            // Ref: http://www.imagemagick.org/Usage/resize/
+            $expandOption = ($EXPAND) ? '' : '\>';
+
+            if ($HEIGHT == -1) {
+                $HEIGHT = 10000;
+            }
+            $this->_resize .= '-resize ' . $WIDTH . 'x' . $HEIGHT . $expandOption . ' ';
+
+        } else if ($MODE == 'force') {
+            if ($HEIGHT == -1) {
+                $HEIGHT = $WIDTH;
+            }
+            $this->_resize .= '-resize ' . $WIDTH . 'x' . $HEIGHT . '\! ';
+        }
+    }
+
+    protected function _normalizeExt($ext)
+    {
+        return mb_strtolower(trim($ext, '. '));
+    }
+
+
+    public function guessOutputFormat($ext)
+    {
+        $ext = $this->_normalizeExt($ext);
+        if (in_array($ext, $this->_acceptWriteExts)) {
+            return $ext;
+        }
+        return $this->_acceptWriteExts[0];
+    }
+
+    public function output($type = null, $file = null, $qual = 90)
+    {
+        if (is_null($type)) {
+            $type = $this->guessOutputFormat($type);
+        }
+        if ($file && is_writable(dirname($file))) {
+            $input = addcslashes($this->_f, ' ');
+            $cleanInput = false;
+            if ($type == 'png') {
+                if ($this->_background == '') {
+                    $this->_background = '-background ' . $this->_back('none');
+                }
+            } elseif ($type == 'jpg' || $type == 'jpeg') {
+                if ($this->_background == '') {
+                    $this->_background = '-background ' . $this->_back('white');
+                }
+                $this->_quality = '-quality ' . $qual;
+                $this->_background .= ' -flatten ';
+            } elseif ($type == 'gif') {
+                // Permit to resize a animated gif
+                // http://askubuntu.com/a/257848
+                $convert = new CommandLine($this->_exec);
+                $temp = Files::tempnam();
+                $convert->setManualArg('"' . $this->_f . '" -coalesce ' . $temp);
+                $input = $temp;
+                $cleanInput = true;
+            }
+
+
+            $args = array($this->_background, '"' . $this->_f . '"', $this->_resize, $this->_quality, $file);
+
+            $this->im = new CommandLine($this->_exec);
+            foreach ($args as $a) {
+                $a = trim($a);
+                if ($a == '') {
+                    continue;
+                }
+                $this->im->setManualArg($a);
+            }
+            $this->im->execute();
+            $this->im->debug();
+
+            if ($cleanInput) {
+                unlink($input);
+            }
+        }
+        return false;
+    }
+
+    protected function _getGravity($H, $V)
+    {
+        $s = '';
+        $p = '';
+        if ($H == 'L') {
+            $s = 'West';
+        } else if ($H == 'R') {
+            $s = 'East';
+        }
+
+        if ($V == 'T') {
+            $p = 'North';
+        } else if ($V == 'B') {
+            $p = 'South';
+        }
+
+        $gravity = $p . $s;
+        if ($gravity == '') {
+            $gravity = 'Center';
+        }
+        return $gravity;
+    }
+
+    protected function _back($color)
+    {
+        $color = trim($color, '\'');
+        if (substr($color, 0, 1) == '#') {
+            return '\'' . $color . '\'';
+        }
+        if (substr($color, 0, 3) == 'rgb') {
+            $color = str_replace('(', '\(', $color);
+            $color = str_replace(')', '\)', $color);
+            return $color;
+        }
+        return $color;
+    }
+
+}
+
+