From 6ea4c232a8a3b00579f4fcb525d7c9e9c9997684 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 22 Aug 2022 16:19:07 +0200 Subject: [PATCH] wip #5408 @0.25 --- src/Graphics/Resizer.php | 183 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 src/Graphics/Resizer.php diff --git a/src/Graphics/Resizer.php b/src/Graphics/Resizer.php new file mode 100644 index 0000000..44981bd --- /dev/null +++ b/src/Graphics/Resizer.php @@ -0,0 +1,183 @@ +_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; + } + +} + + -- 2.39.5