Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

Crop::SetWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 2
b 0
f 1
1
<?php
2
/**
3
 * Crop
4
 *
5
 * @Author: Jens Kooij
6
 * @Version: 1.0
7
 * @package: JNS MVC
8
 * @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported
9
 */
10
 
11
namespace library\images\methods
12
{
13
	use \library\images\IMethod;
14
15
	class Crop extends IMethod
16
	{
17
		protected $_width;
18
		protected $_height;
19
		protected $_x;
20
		protected $_y;
21
		
22
		protected $_destWidth;
23
		protected $_destHeight;
24
		protected $_destX = 0.0;
25
		protected $_destY = 0.0;
26
27
		/**
28
		 * @var null|array
29
		 */
30
		protected $_backgroundColor = null;
31
	
32
		public function init()
33
		{}
34
		
35
		/**
36
		 * Set the width
37
		 *
38
		 * @param  int $width
39
		 * @return self
40
		 */
41
		public function SetWidth($width)
42
		{
43
			$this->_width = intval($width);
44
			$this->_destWidth = intval($width);
45
			return $this;
46
		}
47
		
48
		/**
49
		 * Set the height
50
		 *
51
		 * @param  int $height
52
		 * @return self
53
		 */
54
		public function SetHeight($height)
55
		{
56
			$this->_height = intval($height);
57
			$this->_destHeight = intval($height);
58
			return $this;
59
		}
60
		
61
		/**
62
		 * Set the x
63
		 *
64
		 * @param  int $x
65
		 * @return self
66
		 */
67
		public function SetX($x)
68
		{
69
			$this->_x = $x;
70
			return $this;
71
		}
72
		
73
		/**
74
		 * Set the y
75
		 *
76
		 * @param  int $y
77
		 * @return self
78
		 */
79
		public function SetY($y)
80
		{
81
			$this->_y = $y;
82
			return $this;
83
		}
84
85
		/**
86
		 * If neccesary, fill the background color
87
		 * with this color
88
		 *
89
		 * @param int $r Red
90
		 * @param int $g Green
91
		 * @param int $b Blue
92
		 *
93
		 * @return $this
94
		 */
95
		public function FillBackground($r, $g, $b)
96
		{
97
			$this->_backgroundColor = array(intval($r), intval($g), intval($b));
98
			return $this;
99
		}
100
101
		/**
102
		 * @param resource $imageResource
103
		 *
104
		 * @return resource
105
		 */
106
		public function Execute($imageResource)
107
		{
108
			// Create the new image
109
			$new = imagecreatetruecolor($this->_width, $this->_height);
110
			
111 View Code Duplication
			if ($this->_backgroundColor !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
				$fill = imagecolorallocate($new, $this->_backgroundColor[0], $this->_backgroundColor[1], $this->_backgroundColor[2]);
113
				imagefill($new, 0, 0, $fill);
114
			}
115
116
			// Preserve transparency
117
			imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
118
            imagealphablending($new, false);
119
            imagesavealpha($new, true);
120
			
121
			imagecopyresampled($new, $imageResource, $this->_destX, $this->_destY, $this->_x, $this->_y, $this->_destWidth, $this->_destHeight, $this->_destWidth, $this->_destHeight);
122
			
123
			return $new;
124
		}
125
	}
126
}