Created
August 7, 2011 10:26
-
-
Save kemo/1130278 to your computer and use it in GitHub Desktop.
Kohana_Image_GD with grayscale conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
class Image_GD extends Kohana_Image_GD { | |
public function grayscale() | |
{ | |
// Creating the Canvas | |
$bwimage = $this->_create($this->width, $this->height); | |
$this->_load_image(); | |
//Creates the 256 color palette | |
$palette = array(); | |
for ($c = 0; $c < 256; $c++) | |
{ | |
$palette[$c] = imagecolorallocate($bwimage, $c, $c, $c); | |
} | |
//Reads the original colors pixel by pixel | |
for ($y = 0; $y < $this->height; $y++) | |
{ | |
for ($x = 0; $x < $this->width; $x++) | |
{ | |
$rgb = imagecolorat($this->_image, $x, $y); | |
$r = ($rgb >> 16) & 0xFF; | |
$g = ($rgb >> 8) & 0xFF; | |
$b = $rgb & 0xFF; | |
// This is where we actually use yiq to modify our rgb values, and then convert them to our grayscale palette | |
$gs = $this->yiq($r, $g, $b); | |
imagesetpixel($bwimage, $x, $y, $palette[$gs]); | |
} | |
} | |
imagecopy($this->_image, $bwimage, 0, 0, 0, 0, $this->width, $this->height); | |
imagedestroy($this->_image); | |
$this->_image = $bwimage; | |
return $this; | |
} | |
protected function yiq($r, $g, $b) | |
{ | |
return (($r * 0.299) + ($g * 0.587) + ($b * 0.114)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment