Skip to content

Instantly share code, notes, and snippets.

@nateabele
Created April 13, 2012 20:59
Show Gist options
  • Save nateabele/2380165 to your computer and use it in GitHub Desktop.
Save nateabele/2380165 to your computer and use it in GitHub Desktop.
Proof-of-concept sepia tone filter for Imagine. My math is probably rong.
<?php
namespace moneygram\extensions\imagine;
use Imagine\Image\Box;
use Imagine\Image\Point;
use Imagine\Image\Color;
class SepiaToneFilter implements \Imagine\Filter\FilterInterface {
protected $_imagine;
public function __construct(\Imagine\Image\ImagineInterface $imagine) {
$this->_imagine = $imagine;
}
public function apply(\Imagine\Image\ImageInterface $image) {
$new = $this->_imagine->create($image->getSize(), new Color('fff'));
for ($x = 0; $x < $image->getSize()->getWidth(); $x++) {
for ($y = 0; $y < $image->getSize()->getHeight(); $y++) {
$position = new Point($x, $y);
$pixel = $image->getColorAt($position);
$r = $pixel->getRed();
$g = $pixel->getGreen();
$b = $pixel->getBlue();
$r = min(array((0.272 * $r) + (0.534 * $g) + (0.131 * ($b)), 255));
$g = min(array((0.349 * $r) + (0.686 * $g) + (0.168 * ($b)), 255));
$b = min(array((0.393 * $r) + (0.769 * $g) + (0.189 * ($b)), 255));
$r = ($r > 255) ? 255 : $r;
$g = ($g > 255) ? 255 : $g;
$b = ($b > 255) ? 255 : $b;
$pixel = new Color(array($r, $g, $b));
$new->draw()->dot($position, $pixel);
}
}
return $new;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment