1<?php 2 3/* 4 * This file is part of the Imagine package. 5 * 6 * (c) Bulat Shakirzyanov <mallluhuct@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Imagine\Gd; 13 14use Imagine\Effects\EffectsInterface; 15use Imagine\Exception\RuntimeException; 16use Imagine\Image\Palette\Color\ColorInterface; 17use Imagine\Image\Palette\Color\RGB as RGBColor; 18 19/** 20 * Effects implementation using the GD library 21 */ 22class Effects implements EffectsInterface 23{ 24 private $resource; 25 26 public function __construct($resource) 27 { 28 $this->resource = $resource; 29 } 30 31 /** 32 * {@inheritdoc} 33 */ 34 public function gamma($correction) 35 { 36 if (false === imagegammacorrect($this->resource, 1.0, $correction)) { 37 throw new RuntimeException('Failed to apply gamma correction to the image'); 38 } 39 40 return $this; 41 } 42 43 /** 44 * {@inheritdoc} 45 */ 46 public function negative() 47 { 48 if (false === imagefilter($this->resource, IMG_FILTER_NEGATE)) { 49 throw new RuntimeException('Failed to negate the image'); 50 } 51 52 return $this; 53 } 54 55 /** 56 * {@inheritdoc} 57 */ 58 public function grayscale() 59 { 60 if (false === imagefilter($this->resource, IMG_FILTER_GRAYSCALE)) { 61 throw new RuntimeException('Failed to grayscale the image'); 62 } 63 64 return $this; 65 } 66 67 /** 68 * {@inheritdoc} 69 */ 70 public function colorize(ColorInterface $color) 71 { 72 if (!$color instanceof RGBColor) { 73 throw new RuntimeException('Colorize effects only accepts RGB color in GD context'); 74 } 75 76 if (false === imagefilter($this->resource, IMG_FILTER_COLORIZE, $color->getRed(), $color->getGreen(), $color->getBlue())) { 77 throw new RuntimeException('Failed to colorize the image'); 78 } 79 80 return $this; 81 } 82 83 /** 84 * {@inheritdoc} 85 */ 86 public function sharpen() 87 { 88 $sharpenMatrix = array(array(-1,-1,-1), array(-1,16,-1), array(-1,-1,-1)); 89 $divisor = array_sum(array_map('array_sum', $sharpenMatrix)); 90 91 if (false === imageconvolution($this->resource, $sharpenMatrix, $divisor, 0)) { 92 throw new RuntimeException('Failed to sharpen the image'); 93 } 94 95 return $this; 96 } 97 98 /** 99 * {@inheritdoc} 100 */ 101 public function blur($sigma = 1) 102 { 103 if (false === imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR)) { 104 throw new RuntimeException('Failed to blur the image'); 105 } 106 107 return $this; 108 } 109} 110