|
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 CloudControl\Cms\images\methods { |
|
12
|
|
|
|
|
13
|
|
|
use CloudControl\Cms\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; |
|
31
|
|
|
|
|
32
|
|
|
public function init() |
|
33
|
|
|
{ |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the width |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $width |
|
40
|
|
|
* @return self |
|
41
|
|
|
*/ |
|
42
|
|
|
public function SetWidth($width) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->_width = (int)$width; |
|
45
|
|
|
$this->_destWidth = (int)$width; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the height |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $height |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
|
|
public function SetHeight($height) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->_height = (int)$height; |
|
58
|
|
|
$this->_destHeight = (int)$height; |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the x |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $x |
|
66
|
|
|
* @return self |
|
67
|
|
|
*/ |
|
68
|
|
|
public function SetX($x) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->_x = $x; |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set the y |
|
76
|
|
|
* |
|
77
|
|
|
* @param int $y |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
|
|
public function SetY($y) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->_y = $y; |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* If neccesary, fill the background color |
|
88
|
|
|
* with this color |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $r Red |
|
91
|
|
|
* @param int $g Green |
|
92
|
|
|
* @param int $b Blue |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function FillBackground($r, $g, $b) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->_backgroundColor = array((int)$r, (int)$g, (int)$b); |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param resource $imageResource |
|
104
|
|
|
* |
|
105
|
|
|
* @return resource |
|
106
|
|
|
*/ |
|
107
|
|
|
public function Execute($imageResource) |
|
108
|
|
|
{ |
|
109
|
|
|
// Create the new image |
|
110
|
|
|
$new = imagecreatetruecolor($this->_width, $this->_height); |
|
111
|
|
|
|
|
112
|
|
|
if ($this->_backgroundColor !== null) { |
|
113
|
|
|
$fill = imagecolorallocate($new, $this->_backgroundColor[0], $this->_backgroundColor[1], |
|
|
|
|
|
|
114
|
|
|
$this->_backgroundColor[2]); |
|
115
|
|
|
imagefill($new, 0, 0, $fill); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Preserve transparency |
|
119
|
|
|
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
|
|
|
|
|
|
120
|
|
|
imagealphablending($new, false); |
|
|
|
|
|
|
121
|
|
|
imagesavealpha($new, true); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
imagecopyresampled($new, $imageResource, $this->_destX, $this->_destY, $this->_x, $this->_y, |
|
|
|
|
|
|
124
|
|
|
$this->_destWidth, $this->_destHeight, $this->_destWidth, $this->_destHeight); |
|
125
|
|
|
|
|
126
|
|
|
return $new; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |