Completed
Push — master ( 2b7a2e...74ad36 )
by Michael
11:15
created

AsGrayscale::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 7
nc 3
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
 * AsGrayscale operation class
30
 * 
31
 * @package Internal/Operations
32
 */
33
class AsGrayscale
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
		$new = $image->asTrueColor();
44
		
45
		if (!imagefilter($new->getHandle(), IMG_FILTER_GRAYSCALE)) {
46
			throw new GDFunctionResultException("imagefilter() returned false");
47
		}
48
		
49
		if (!$image->isTrueColor()) {
50
			$new = $new->asPalette();
51
		}
52
		
53
		return $new;
54
	}
55
}
56