From 188f8cd64d137b9db9588101df729508e35645c9 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 24 Nov 2014 09:27:47 +0100 Subject: [PATCH] Adding option taking comma-separated list of 11 float-values, wraps and exposes . #4 --- CImage.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++-- README.md | 1 + webroot/img.php | 12 +++++++- 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/CImage.php b/CImage.php index de49a11..6c2c854 100644 --- a/CImage.php +++ b/CImage.php @@ -187,6 +187,13 @@ class CImage public $cropOrig; // Save original value + /** + * Array with details on how to do image convolution. + */ + private $convolve; + private $convolveString = null; // Original argument, if existing + + /** * Properties (clean up these) */ @@ -354,6 +361,7 @@ class CImage 'sharpen' => null, 'emboss' => null, 'blur' => null, + 'convolve' => null, 'rotateAfter' => null, 'autoRotate' => false, @@ -399,13 +407,44 @@ class CImage if (isset($parts[$i])) { $filter["arg{$i}"] = $parts[$i]; } else { - throw new Exception('Missing arg to filter, review how many arguments are needed at http://php.net/manual/en/function.imagefilter.php'); + throw new Exception( + 'Missing arg to filter, review how many arguments are needed at + http://php.net/manual/en/function.imagefilter.php' + ); } } $args['filters'][$key] = $filter; } } + // Convert convolve settings from string to array + if (isset($args['convolve']) && !is_array($args['convolve'])) { + $part = explode(',', $args['convolve']); + $this->convolveString = $args['convolve']; + + if (count($part) != 11) { + throw new Exception( + 'Missmatch in argument convolve. Expected comma-separated string with 11 float values.' + ); + } + + array_walk($part, function ($item, $key) { + if (!is_numeric($item)) { + throw new Exception("Argument convole should be float but is not."); + } + }); + + $args['convolve'] = array( + 'matrix' => array( + array($part[0], $part[1], $part[2]), + array($part[3], $part[4], $part[5]), + array($part[6], $part[7], $part[8]), + ), + 'div' => $part[9], + 'offset' => $part[10], + ); + } + // Merge default arguments with incoming and set properties. //$args = array_merge_recursive($defaults, $args); $args = array_merge($defaults, $args); @@ -528,7 +567,7 @@ class CImage $this->log("Setting new height based on aspect ratio to {$this->newHeight}"); } - // Change width & height based on dpr + // Change width & height based on dpr if ($this->dpr != 1) { if (!is_null($this->newWidth)) { $this->newWidth = round($this->newWidth * $this->dpr); @@ -778,6 +817,7 @@ class CImage && !$this->sharpen && !$this->emboss && !$this->blur + && !$this->convolve && !$this->palette && !$this->quality && !$this->compress @@ -855,12 +895,18 @@ class CImage $optimize .= $this->pngDeflate ? 'd' : null; } + $convolve = null; + if ($this->convolve) { + $convolve = 'convolve' . str_replace(',', '', $this->convolveString); + } + $subdir = str_replace('/', '-', dirname($this->imageSrc)); $subdir = ($subdir == '.') ? '_.' : $subdir; $file = $subdir . '_' . $parts['filename'] . '_' . round($this->newWidth) . '_' . round($this->newHeight) . $offset . $crop . $cropToFit . $crop_x . $crop_y . $quality . $filters . $sharpen . $emboss . $blur . $palette . $optimize - . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . '.' . $this->extension; + . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor . $convolve + . '.' . $this->extension; return $this->setTarget($file, $base); } @@ -1244,6 +1290,12 @@ class CImage $this->sharpenImage(); } + // Custom convolution + if ($this->convolve) { + $this->log("Convolve: " . $this->convolveString); + $this->imageConvolution(); + } + return $this; } @@ -1412,6 +1464,30 @@ class CImage + /** + * Image convolution. + * + * @param array $matrix A 3x3 matrix: an array of three arrays of three floats. + * @param float $div The divisor of the result of the convolution, used for normalization. + * @param float $offset Color offset. + * + * @return $this + */ + public function imageConvolution($matrix = null, $div = null, $offset = null) + { + if ($matrix == null && $this->convolve) { + $matrix = $this->convolve['matrix']; + $div = $this->convolve['div']; + $offset = $this->convolve['offset']; + } + + imageconvolution($this->image, $matrix, $div, $offset); + + return $this; + } + + + /** * Create a image and keep transparency for png and gifs. * diff --git a/README.md b/README.md index 1014452..84532cd 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Revision history v0.5.x (latest) +* Adding option `convolve` taking comma-separated list of 11 float-values, wraps and exposes `imageconvoluttion()`. #4 * Adding option `dpr, device-pixel-ratio` which defaults to 1. Set to 2 to get a twice as large image. Useful for Retina displays. Basically a shortcut to enlarge the image. * Adding utility `cache.bash` to ease gathering stats on cache usage. #21 * Cache-directory can now be readonly and serve all cached files, still failing when need to save files. #5 diff --git a/webroot/img.php b/webroot/img.php index 6fc2c9a..bea2214 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -472,6 +472,15 @@ verbose("dpr = $dpr"); +/** + * convolve - image convolution as in http://php.net/manual/en/function.imageconvolution.php + */ +$convolve = get('convolve', null); + +verbose("convolve = $convolve"); + + + /** * Display image if verbose mode */ @@ -483,7 +492,7 @@ if ($verbose) { unset($query['nocache']); unset($query['nc']); unset($query['json']); - $url1 = '?' . http_build_query($query); + $url1 = '?' . htmlentities(urldecode(http_build_query($query))); echo <<$url1
@@ -526,6 +535,7 @@ $img->setVerbose($verbose) 'sharpen' => $sharpen, 'emboss' => $emboss, 'blur' => $blur, + 'convolve' => $convolve, 'rotateAfter' => $rotateAfter, 'autoRotate' => $autoRotate,