1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Water mark |
4
|
|
|
* Adds a watermark to an image |
5
|
|
|
* |
6
|
|
|
* @Author: Jens Kooij |
7
|
|
|
* @Version: 1.0 |
8
|
|
|
* @package: JNS MVC |
9
|
|
|
* @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CloudControl\Cms\images\methods { |
13
|
|
|
|
14
|
|
|
use CloudControl\Cms\images\Image; |
15
|
|
|
use CloudControl\Cms\images\IMethod; |
16
|
|
|
|
17
|
|
|
class Watermark extends IMethod |
18
|
|
|
{ |
19
|
|
|
protected $_x = 0; |
20
|
|
|
protected $_y = 0; |
21
|
|
|
protected $_transparency = 100; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Image |
25
|
|
|
*/ |
26
|
|
|
protected $_watermark; |
27
|
|
|
|
28
|
|
|
protected function init() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets transparency for watermark |
34
|
|
|
* |
35
|
|
|
* @param int $transparency |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
|
|
public function SetTransparency($transparency) |
39
|
|
|
{ |
40
|
|
|
$this->_transparency = (int)$transparency; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Use build-in logic to position the watermark |
46
|
|
|
* |
47
|
|
|
* @param string $x |
48
|
|
|
* @param string $y |
49
|
|
|
* @return self |
50
|
|
|
*/ |
51
|
|
|
public function SetPosition($x, $y) |
52
|
|
|
{ |
53
|
|
|
$this->SetX($x); |
54
|
|
|
$this->SetY($y); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Use build-in logic to position the x of watermark |
60
|
|
|
* |
61
|
|
|
* @param resource $imageResource |
62
|
|
|
* @return int |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
protected function calculateX($imageResource) |
66
|
|
|
{ |
67
|
|
|
if ((int)$this->_x === $this->_x) { |
68
|
|
|
return $this->_x; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$x = strtolower($this->_x); |
72
|
|
|
|
73
|
|
|
$imageWidth = imagesx($imageResource); |
74
|
|
|
$watermarkWidth = imagesx($this->GetWatermark()->getImageResource()); |
75
|
|
|
|
76
|
|
|
if ($x == 'left') { |
77
|
|
|
$x = 0; |
78
|
|
|
} elseif ($x == 'center') { |
79
|
|
|
$x = $imageWidth / 2 - ($watermarkWidth / 2); |
80
|
|
|
} elseif ($x == 'right') { |
81
|
|
|
$x = $imageWidth - $watermarkWidth; |
82
|
|
|
} |
83
|
|
|
return (int)$x; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Use build-in logic to position the y of watermark |
88
|
|
|
* |
89
|
|
|
* @param resource $imageResource |
90
|
|
|
* @return int |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function calculateY($imageResource) |
94
|
|
|
{ |
95
|
|
|
if ((int)$this->_y === $this->_y) { |
96
|
|
|
return $this->_y; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$y = strtolower($this->_y); |
100
|
|
|
|
101
|
|
|
$imageHeight = imagesy($imageResource); |
102
|
|
|
$watermarkHeight = imagesy($this->GetWatermark()->getImageResource()); |
103
|
|
|
|
104
|
|
|
if ($y == 'top') { |
105
|
|
|
$y = 0; |
106
|
|
|
} elseif ($y == 'center') { |
107
|
|
|
$y = $imageHeight / 2 - ($watermarkHeight / 2); |
108
|
|
|
} elseif ($y == 'bottom') { |
109
|
|
|
$y = $imageHeight - $watermarkHeight; |
110
|
|
|
} |
111
|
|
|
return (int)$y; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets the image that will be used as watermark |
116
|
|
|
* |
117
|
|
|
* @param Image $image |
118
|
|
|
* @return Watermark |
119
|
|
|
*/ |
120
|
|
|
public function SetWatermark(Image $image) |
121
|
|
|
{ |
122
|
|
|
$this->_watermark = $image; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the watermark. |
128
|
|
|
* Throws an Exception if it's not set or if it's not an \CloudControl\Cms\image\Image |
129
|
|
|
* @return Image |
130
|
|
|
* @throws \Exception |
131
|
|
|
*/ |
132
|
|
|
public function GetWatermark() |
133
|
|
|
{ |
134
|
|
|
if ($this->_watermark == null) { |
135
|
|
|
throw new \Exception('A watermark is not set. Please supply a \CloudControl\Cms\image\Image using $this->SetWatermark'); |
136
|
|
|
} |
137
|
|
|
return $this->_watermark; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the x |
142
|
|
|
* |
143
|
|
|
* @param int | string $x |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
|
|
public function SetX($x) |
147
|
|
|
{ |
148
|
|
|
$this->_x = $x; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the y |
154
|
|
|
* |
155
|
|
|
* @param int | string $y |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
|
|
public function SetY($y) |
159
|
|
|
{ |
160
|
|
|
$this->_y = $y; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param resource $imageResource |
166
|
|
|
* @return resource |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
public function Execute($imageResource) |
170
|
|
|
{ |
171
|
|
|
$watermark = $this->GetWatermark(); |
172
|
|
|
$watermarkWidth = imagesx($watermark->getImageResource()); |
173
|
|
|
$watermarkHeight = imagesy($watermark->getImageResource()); |
174
|
|
|
|
175
|
|
|
$x = $this->calculateX($imageResource); |
176
|
|
|
$y = $this->calculateY($imageResource); |
177
|
|
|
|
178
|
|
|
$imageWidth = imagesx($imageResource); |
179
|
|
|
$imageHeight = imagesy($imageResource); |
180
|
|
|
|
181
|
|
|
$new = imagecreatetruecolor($imageWidth, $imageHeight); |
182
|
|
|
|
183
|
|
|
// Preserve transparency of the image |
184
|
|
|
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
|
|
|
|
185
|
|
|
imagealphablending($new, false); |
|
|
|
|
186
|
|
|
imagesavealpha($new, true); |
|
|
|
|
187
|
|
|
|
188
|
|
|
// Preserve transparency of the watermark |
189
|
|
|
imagecolortransparent($watermark->getImageResource(), |
190
|
|
|
imagecolorallocatealpha($watermark->getImageResource(), 0, 0, 0, 127)); |
191
|
|
|
imagealphablending($watermark->getImageResource(), false); |
192
|
|
|
imagesavealpha($watermark->getImageResource(), true); |
193
|
|
|
|
194
|
|
|
imagealphablending($new, true); |
195
|
|
|
imagealphablending($watermark->getImageResource(), true); |
196
|
|
|
|
197
|
|
|
imagecopy($new, $imageResource, 0, 0, 0, 0, $imageWidth, $imageHeight); |
|
|
|
|
198
|
|
|
imagecopymerge($new, $watermark->getImageResource(), $x, $y, 0, 0, $watermarkWidth, $watermarkHeight, |
|
|
|
|
199
|
|
|
$this->_transparency); |
200
|
|
|
|
201
|
|
|
return $new; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |