public function filter($resource) { if (!is_resource($resource) || 'gd' !== get_resource_type($resource)) { throw new \Exception('Resource must be given'); } $width = imagesx($resource); $height = imagesy($resource); $greyscaleImageResource = imagecreatetruecolor($width, $height); // prepare greyscale palette for ($c = 0; $c < 256; $c++) { $palette[$c] = imagecolorallocate($greyscaleImageResource, $c, $c, $c); } // set pixels for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $rgb = imagecolorat($resource, $x, $y); $grey = Yiq::getYFromRgbArray(Rgb::fromIntAsArray($rgb)); imagesetpixel($greyscaleImageResource, $x, $y, $palette[$grey]); } } return $greyscaleImageResource; }
public function testGreyscale() { $image = $this->_factory->openImage(__DIR__ . '/test.png'); $this->_factory->filterImage($image, 'greyscale'); $color = imagecolorat($image->getResource(), 0, 0); $this->assertEquals(array(29, 29, 29), Rgb::fromIntAsArray($color)); $color = imagecolorat($image->getResource(), 0, 199); $this->assertEquals(array(225, 225, 225), Rgb::fromIntAsArray($color)); }