Passed
Push — master ( b5dddf...91d417 )
by Richard
09:12
created

AsNegative::execute()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
rs 9.2222
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php
2
	/**
3
##DOC-SIGNATURE##
4
5
    This file is part of WideImage.
6
		
7
    WideImage is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU Lesser General Public License as published by
9
    the Free Software Foundation; either version 2.1 of the License, or
10
    (at your option) any later version.
11
		
12
    WideImage is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16
		
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with WideImage; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21
    * @package Internal/Operations
22
  **/
23
24
namespace WideImage\Operation;
25
26
use WideImage\Exception\GDFunctionResultException;
27
28
/**
29
 * AsNegative operation class
30
 * 
31
 * @package Internal/Operations
32
 */
33
class AsNegative
34
{
35
	/**
36
	 * Returns a greyscale copy of an image
37
	 *
38
	 * @param \WideImage\Image $image
39
	 * @return \WideImage\Image
40
	 */
41
	public function execute($image)
42
	{
43
		$palette     = !$image->isTrueColor();
44
		$transparent = $image->isTransparent();
45
		
46
		if ($palette && $transparent) {
47
			$tcrgb = $image->getTransparentColorRGB();
48
		}
49
		
50
		$new = $image->asTrueColor();
51
		
52
		if (!imagefilter($new->getHandle(), IMG_FILTER_NEGATE)) {
53
			throw new GDFunctionResultException("imagefilter() returned false");
54
		}
55
		
56
		if ($palette) {
57
			$new = $new->asPalette();
58
			
59
			if ($transparent) {
60
				$irgb = array('red' => 255 - $tcrgb['red'], 'green' => 255 - $tcrgb['green'], 'blue' => 255 - $tcrgb['blue'], 'alpha' => 127);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tcrgb does not seem to be defined for all execution paths leading up to this point.
Loading history...
61
				
62
				// needs imagecolorexactalpha instead of imagecolorexact, otherwise doesn't work on some transparent GIF images
63
				$new_tci = imagecolorexactalpha($new->getHandle(), $irgb['red'], $irgb['green'], $irgb['blue'], 127);
64
				$new->setTransparentColor($new_tci);
65
			}
66
		}
67
		
68
		return $new;
69
	}
70
}
71