Skip to content

Commit

Permalink
fixed issue with builder compiling canvas only filters. added sepia, …
Browse files Browse the repository at this point in the history
…colorize, pixelate, and sharpen filters
  • Loading branch information
Eddie committed Jul 22, 2014
1 parent 086489f commit c8bdfad
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 18 deletions.
2 changes: 1 addition & 1 deletion demo/PinkFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PinkFilter extends BaseFilter {

public static function getFilterMask()
{
return 'pink';
return '/^pink/';
}

public function filter($add = false)
Expand Down
16 changes: 11 additions & 5 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
</head>
<body>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/overlay:2/">
<img src="/image-builder/chipmunks-are-awesome.png/" title="No filters." alt="No Filters."> No filters
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/overlay:2/pink:unicorn/">
<img src="/image-builder/chipmunks-are-awesome.png/overlay:6/" title="Creates a symmetrical grid of mirror images overlayed on eachother" alt="Creates a grid of mirror images overlayed on eachother"> Creates a grid of mirror images overlayed on eachother
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/250x250/overlay:3/pink:unicorn/">
<img src="/image-builder/chipmunks-are-awesome.png/250x250/overlay:3/pink:unicorn/" title="Multiple filters stack effects left to right" alt="Multiple filters stack effects left to right"> Multiple filters stack effects left to right
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/pink:unicorn/250x250/overlay:2">
<img src="/image-builder/chipmunks-are-awesome.png/colorize:20:80:1" title="Green me up cap'n!" alt="Green me up!"> Green me up cap'n!
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/">
<img src="/image-builder/chipmunks-are-awesome.png/sharpen:100" title="It's important to look sharp" alt="It's important to look sharp"> It's important to look sharp
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/sepia:100" title="Old school" alt="Old school"> Old school
</div>
<div>
<img src="/image-builder/chipmunks-are-awesome.png/pixelate:6" title="8 bit" alt="8 bit"> 8 bit
</div>
</body>
</html>
27 changes: 17 additions & 10 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,37 @@ 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'])) {
// build args off parsed filter
$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;
}

Expand Down
30 changes: 30 additions & 0 deletions src/Filter/Colorize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace stratease\ImagiFly\Filter;
use stratease\ImagiFly\Filter\BaseFilter;
use Intervention\Image\ImageManagerStatic;

class Colorize extends BaseFilter {
public static function getFilterMask() {
return "/^colorize/";
}

/**
* A number between -100 through 100. Negatives will subtract the given color from the canvas.
* @param int $r
* @param int $g
* @param int $b
* @return mixed
*/
public function filter($r, $g, $b)
{
$this->canvas->colorize($r, $g, $b);

return $this->canvas;
}

public function writesBaseImageToCanvas()
{
return false;
}
}
28 changes: 28 additions & 0 deletions src/Filter/Pixelate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace stratease\ImagiFly\Filter;
use stratease\ImagiFly\Filter\BaseFilter;
use Intervention\Image\ImageManagerStatic;

class Pixelate extends BaseFilter {

public static function getFilterMask() {
return "/^pixelate/";
}

/**
* @param $pixelSize
* @return mixed
*/
public function filter($pixelSize)
{
$this->canvas->pixelate((int)$pixelSize);

return $this->canvas;
}

public function writesBaseImageToCanvas()
{
return false;
}
}
3 changes: 1 addition & 2 deletions src/Filter/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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;
}

Expand Down
31 changes: 31 additions & 0 deletions src/Filter/Sepia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace stratease\ImagiFly\Filter;
use stratease\ImagiFly\Filter\BaseFilter;
use Intervention\Image\ImageManagerStatic;

class Sepia extends BaseFilter {

public static function getFilterMask() {
return "/^sepia/";
}

/**
* @todo Add an amount arg to tone back the sepia effect?
* @return mixed
*/
public function filter()
{
// ditch pesky colors
$this->canvas->greyscale();
// sepia-ish tone it
$this->canvas->colorize(25, 11, 0);

return $this->canvas;
}

public function writesBaseImageToCanvas()
{
return false;
}
}
28 changes: 28 additions & 0 deletions src/Filter/Sharpen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace stratease\ImagiFly\Filter;
use stratease\ImagiFly\Filter\BaseFilter;
use Intervention\Image\ImageManagerStatic;

class Sharpen extends BaseFilter {
public static function getFilterMask() {
return "/^sharpen/";
}


/**
* @param $amount
* @return mixed
*/
public function filter($amount)
{
$this->canvas->sharpen($amount);

return $this->canvas;
}

public function writesBaseImageToCanvas()
{
return false;
}
}

0 comments on commit c8bdfad

Please sign in to comment.