From 69c8503812bda581ca9ba464f149bbc8a91884fa Mon Sep 17 00:00:00 2001 From: Mike Street Date: Sun, 13 Aug 2017 16:18:50 +0100 Subject: [PATCH 1/3] Add image filters --- README.md | 43 +++++++++++++++++++++++++++++++ lib/ImageResize.php | 63 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/README.md b/README.md index bec3365..4f78e0f 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,49 @@ $image ; ``` +Filters +-------- + +ImageResize allows you to apply filters to the image such as grayscale and colorizing + +Grayscale: + +```php +$image->grayscale(); +``` + +Invert the image colors: + +```php +$image->invert(); +``` + +Pixelate the image: + +```php +$image->pixelate(); +$image->pixelate($amount = 20); +``` + +Colorize: + +`$red`, `$green` and `$blue` variables aree all between 0 and 255. `$alpha` is between 0 (transparent) and 100 (opaque) + +```php +$image->colorize($red = 255, $green = 10, $blue = 80, $alpha = 50); +``` + +Filters can be chained too: + +```php +$image = new ImageResize('image.jpg'); +$image + ->invert() + ->pixelate(2) + ->grayscale(); +``` + + Exceptions -------- diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 1410b72..704d246 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -23,6 +23,8 @@ class ImageResize public $source_type; + private $filter = array(); + protected $source_image; protected $original_w; @@ -214,6 +216,13 @@ public function save($filename, $image_type = null, $quality = null, $permission $this->source_h ); + // If filters have been applied loop through and apply each one with params + if(count($this->filter) > 0) { + foreach($this->filter as $filter) { + imagefilter($dest_image, ...$filter); + } + } + switch ($image_type) { case IMAGETYPE_GIF: imagegif($dest_image, $filename); @@ -595,6 +604,60 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER) } return $size; } + + /** + * Makes the image grayscale + * + * @return \static + */ + public function grayscale() + { + $this->filter[] = [IMG_FILTER_GRAYSCALE]; + return $this; + } + + /** + * Inverts the image colors + * + * @return \static + */ + public function invert() + { + $this->filter[] = [IMG_FILTER_NEGATE]; + return $this; + } + + /** + * Pixellates the image by the specified amount + * + * @param integer $amount + * @return \static + */ + public function pixelate($amount = 5) + { + $this->filter[] = [IMG_FILTER_PIXELATE, $amount, false]; + return $this; + } + + /** + * Colorises the image with the specificed rgba color + * + * @param integer $red 0 - 255 + * @param integer $green 0 - 255 + * @param integer $blue 0 - 255 + * @param integer $alpha 0 - 100 + * @return \static + */ + public function colorize($red = 0, $green = 0, $blue = 0, $alpha = 100) + { + // Function has alpha from 0 - 100, but alpha is actually 0 - 127. + $alpha = ($alpha * 127) / 100; + // Alpha is also oposite (1 is opaque and 127 transparent) + $alpha = 127 - $alpha; + + $this->filter[] = [IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha]; + return $this; + } } // imageflip definition for PHP < 5.5 From 6833ed9975bfe228396c958c4e21b270f69d73ff Mon Sep 17 00:00:00 2001 From: Mike Street Date: Mon, 14 Aug 2017 12:13:39 +0100 Subject: [PATCH 2/3] Replace propose upacking with call_user_func_array --- lib/ImageResize.php | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 704d246..a0fd654 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -41,7 +41,7 @@ class ImageResize protected $source_w; protected $source_h; - + protected $source_info; /** @@ -53,9 +53,9 @@ class ImageResize */ public static function createFromString($image_data) { - if(empty($image_data) || $image_data === null) { - throw new ImageResizeException('image_data must not be empty'); - } + if(empty($image_data) || $image_data === null) { + throw new ImageResizeException('image_data must not be empty'); + } $resize = new self('data://application/octet-stream;base64,' . base64_encode($image_data)); return $resize; } @@ -71,14 +71,14 @@ public function __construct($filename) { if($filename === null || empty($filename) || (substr($filename,0,7) !== 'data://' && !is_file($filename))) { - throw new ImageResizeException('File does not exist'); - } - - $finfo = finfo_open(FILEINFO_MIME_TYPE); - if(strstr(finfo_file($finfo, $filename),'image') === false) { - throw new ImageResizeException('Unsupported file type'); - } - + throw new ImageResizeException('File does not exist'); + } + + $finfo = finfo_open(FILEINFO_MIME_TYPE); + if(strstr(finfo_file($finfo, $filename),'image') === false) { + throw new ImageResizeException('Unsupported file type'); + } + if (!$image_info = getimagesize($filename, $this->source_info)) { $image_info = getimagesize($filename); } @@ -126,11 +126,11 @@ public function __construct($filename) // http://stackoverflow.com/a/28819866 public function imageCreateJpegfromExif($filename){ $img = imagecreatefromjpeg($filename); - + if (!function_exists('exif_read_data') || !isset($this->source_info['APP1']) || strpos ($this->source_info['APP1'], 'Exif') !== 0) { return $img; } - + $exif = exif_read_data($filename); if (!$exif || !isset($exif['Orientation'])){ @@ -219,7 +219,8 @@ public function save($filename, $image_type = null, $quality = null, $permission // If filters have been applied loop through and apply each one with params if(count($this->filter) > 0) { foreach($this->filter as $filter) { - imagefilter($dest_image, ...$filter); + array_unshift($filter, $dest_image); + call_user_func_array('imagefilter', $filter); } } @@ -318,7 +319,7 @@ public function resizeToShortSide($max_short, $allow_enlarge = false) $this->resize($max_short, $long, $allow_enlarge); } - + return $this; } @@ -342,7 +343,7 @@ public function resizeToLongSide($max_long, $allow_enlarge = false) $this->resize($max_long, $short, $allow_enlarge); } - + return $this; } @@ -604,7 +605,7 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER) } return $size; } - + /** * Makes the image grayscale * @@ -649,7 +650,7 @@ public function pixelate($amount = 5) * @return \static */ public function colorize($red = 0, $green = 0, $blue = 0, $alpha = 100) - { + { // Function has alpha from 0 - 100, but alpha is actually 0 - 127. $alpha = ($alpha * 127) / 100; // Alpha is also oposite (1 is opaque and 127 transparent) From e5a11213d1080b98d18fdb74dde1e6e8e1273237 Mon Sep 17 00:00:00 2001 From: Mike Street Date: Mon, 14 Aug 2017 12:18:16 +0100 Subject: [PATCH 3/3] Update array syntax for backward compat --- lib/ImageResize.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ImageResize.php b/lib/ImageResize.php index a0fd654..bf96782 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -613,7 +613,7 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER) */ public function grayscale() { - $this->filter[] = [IMG_FILTER_GRAYSCALE]; + $this->filter[] = array(IMG_FILTER_GRAYSCALE); return $this; } @@ -624,7 +624,7 @@ public function grayscale() */ public function invert() { - $this->filter[] = [IMG_FILTER_NEGATE]; + $this->filter[] = array(IMG_FILTER_NEGATE); return $this; } @@ -636,7 +636,7 @@ public function invert() */ public function pixelate($amount = 5) { - $this->filter[] = [IMG_FILTER_PIXELATE, $amount, false]; + $this->filter[] = array(IMG_FILTER_PIXELATE, $amount, false); return $this; } @@ -656,7 +656,7 @@ public function colorize($red = 0, $green = 0, $blue = 0, $alpha = 100) // Alpha is also oposite (1 is opaque and 127 transparent) $alpha = 127 - $alpha; - $this->filter[] = [IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha]; + $this->filter[] = array(IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha); return $this; } }