From c8bdfad1f055afa0f4f8960f6d2539aadd8cf221 Mon Sep 17 00:00:00 2001 From: Eddie Date: Tue, 22 Jul 2014 18:55:54 +0000 Subject: [PATCH] fixed issue with builder compiling canvas only filters. added sepia, colorize, pixelate, and sharpen filters --- demo/PinkFilter.php | 2 +- demo/index.html | 16 +++++++++++----- src/Builder.php | 27 +++++++++++++++++---------- src/Filter/Colorize.php | 30 ++++++++++++++++++++++++++++++ src/Filter/Pixelate.php | 28 ++++++++++++++++++++++++++++ src/Filter/Resize.php | 3 +-- src/Filter/Sepia.php | 31 +++++++++++++++++++++++++++++++ src/Filter/Sharpen.php | 28 ++++++++++++++++++++++++++++ 8 files changed, 147 insertions(+), 18 deletions(-) create mode 100755 src/Filter/Colorize.php create mode 100755 src/Filter/Pixelate.php create mode 100755 src/Filter/Sepia.php create mode 100755 src/Filter/Sharpen.php diff --git a/demo/PinkFilter.php b/demo/PinkFilter.php index 9b16368..6cd682d 100755 --- a/demo/PinkFilter.php +++ b/demo/PinkFilter.php @@ -6,7 +6,7 @@ class PinkFilter extends BaseFilter { public static function getFilterMask() { - return 'pink'; + return '/^pink/'; } public function filter($add = false) diff --git a/demo/index.html b/demo/index.html index 172f586..7c00ef1 100755 --- a/demo/index.html +++ b/demo/index.html @@ -5,19 +5,25 @@
- + No Filters. No filters
- + Creates a grid of mirror images overlayed on eachother Creates a grid of mirror images overlayed on eachother
- + Multiple filters stack effects left to right Multiple filters stack effects left to right
- + Green me up! Green me up cap'n!
- + It's important to look sharp It's important to look sharp +
+
+ Old school Old school +
+
+ 8 bit 8 bit
\ No newline at end of file diff --git a/src/Builder.php b/src/Builder.php index 6b1fb39..2cd70d6 100755 --- a/src/Builder.php +++ b/src/Builder.php @@ -235,12 +235,27 @@ public function getFilter($filterName) /** * @param Image $canvas * @param Image $baseImage - * @param $filters array + * @param $filters array Filter name and args to be compiled * @return Image */ public function applyFilters(Image $canvas, Image $baseImage, $filters) { - $baseImageWritten = false; + // first check if we have a filter that applies the base image + $writeBaseImage = true; + foreach($filters as $filter) { + // build filter object... + if($filterObj = $this->getFilter($filter['filter'])) { + if($filterObj->writesBaseImageToCanvas()) { + $writeBaseImage = false; + break; + } + } + } + // if no filters wrote the base image on the canvas, we presume that should always be done first.. do it now. + if($writeBaseImage === true) { + $canvas->insert($baseImage, null, 0, 0); // should we center? + } + // now apply all filters to canvas foreach($filters as $filter) { // build filter object... if($filterObj = $this->getFilter($filter['filter'])) { @@ -248,17 +263,9 @@ public function applyFilters(Image $canvas, Image $baseImage, $filters) $fArgs = $filterObj->buildArgs($filter); // write filter to our current canvas $canvas = $this->applyFilter($canvas, $baseImage, $filterObj, $fArgs); - // check filter flags - if(!$baseImageWritten) { - $baseImageWritten = $filterObj->writesBaseImageToCanvas(); - } } } - // if no filters wrote the base image on the canvas, we presume that should always be done.. do it now. - if($baseImageWritten === false) { - $canvas->insert($baseImage, null, 0, 0); // should we center? - } return $canvas; } diff --git a/src/Filter/Colorize.php b/src/Filter/Colorize.php new file mode 100755 index 0000000..1c7b303 --- /dev/null +++ b/src/Filter/Colorize.php @@ -0,0 +1,30 @@ +canvas->colorize($r, $g, $b); + + return $this->canvas; + } + + public function writesBaseImageToCanvas() + { + return false; + } +} \ No newline at end of file diff --git a/src/Filter/Pixelate.php b/src/Filter/Pixelate.php new file mode 100755 index 0000000..1eaaeee --- /dev/null +++ b/src/Filter/Pixelate.php @@ -0,0 +1,28 @@ +canvas->pixelate((int)$pixelSize); + + return $this->canvas; + } + + public function writesBaseImageToCanvas() + { + return false; + } +} \ No newline at end of file diff --git a/src/Filter/Resize.php b/src/Filter/Resize.php index e3c883c..7885aa2 100755 --- a/src/Filter/Resize.php +++ b/src/Filter/Resize.php @@ -9,7 +9,7 @@ public static function getFilterMask() { } public function filter($width, $height) { - // Resize both the base image + // Resize both the base image.. this enforces all future filters from the base image will reflect the resize done $this->baseImage->resize($width, $height, function($constraint) { $constraint->aspectRatio(); }); @@ -18,7 +18,6 @@ public function filter($width, $height) $constraint->aspectRatio(); }); - // .. this enforces all future filters from the base image will reflect the resize done return $c; } diff --git a/src/Filter/Sepia.php b/src/Filter/Sepia.php new file mode 100755 index 0000000..37d75e7 --- /dev/null +++ b/src/Filter/Sepia.php @@ -0,0 +1,31 @@ +canvas->greyscale(); + // sepia-ish tone it + $this->canvas->colorize(25, 11, 0); + + return $this->canvas; + } + + public function writesBaseImageToCanvas() + { + return false; + } +} \ No newline at end of file diff --git a/src/Filter/Sharpen.php b/src/Filter/Sharpen.php new file mode 100755 index 0000000..fa889e0 --- /dev/null +++ b/src/Filter/Sharpen.php @@ -0,0 +1,28 @@ +canvas->sharpen($amount); + + return $this->canvas; + } + + public function writesBaseImageToCanvas() + { + return false; + } +} \ No newline at end of file