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 library\images\methods |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
use library\images\Image; |
16
|
|
|
use \library\images\IMethod; |
17
|
|
|
|
18
|
|
|
class Watermark extends IMethod |
19
|
|
|
{ |
20
|
|
|
protected $_x = 0; |
21
|
|
|
protected $_y = 0; |
22
|
|
|
protected $_transparency = 100; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \library\images\Image |
26
|
|
|
*/ |
27
|
|
|
protected $_watermark; |
28
|
|
|
|
29
|
|
|
protected function init() |
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 = intval($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 self |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
protected function calculateX($imageResource) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (intval($this->_x) === $this->_x) return $this->_x; |
67
|
|
|
|
68
|
|
|
$x = strtolower($this->_x); |
69
|
|
|
|
70
|
|
|
$imageWidth = imagesx($imageResource); |
71
|
|
|
$watermarkWidth = imagesx($this->GetWatermark()->GetImageResource()); |
72
|
|
|
|
73
|
|
|
if ($x == 'left') { |
74
|
|
|
$x = 0; |
75
|
|
|
} elseif ($x == 'center') { |
76
|
|
|
$x = $imageWidth / 2 - ($watermarkWidth / 2); |
77
|
|
|
} elseif ($x == 'right') { |
78
|
|
|
$x = $imageWidth - $watermarkWidth; |
79
|
|
|
} |
80
|
|
|
return intval($x); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Use build-in logic to position the y of watermark |
85
|
|
|
* |
86
|
|
|
* @param resource $imageResource |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function calculateY($imageResource) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
if (intval($this->_y) === $this->_y) return $this->_y; |
92
|
|
|
|
93
|
|
|
$y = strtolower($this->_y); |
94
|
|
|
|
95
|
|
|
$imageHeight = imagesy($imageResource); |
96
|
|
|
$watermarkHeight = imagesy($this->GetWatermark()->GetImageResource()); |
97
|
|
|
|
98
|
|
|
if ($y == 'top') { |
99
|
|
|
$y = 0; |
100
|
|
|
} elseif ($y == 'center') { |
101
|
|
|
$y = $imageHeight / 2 - ($watermarkHeight / 2); |
102
|
|
|
} elseif ($y == 'bottom') { |
103
|
|
|
$y = $imageHeight - $watermarkHeight; |
104
|
|
|
} |
105
|
|
|
return intval($y); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets the image that will be used as watermark |
110
|
|
|
* |
111
|
|
|
* @param Image $image |
112
|
|
|
* @return Watermark |
113
|
|
|
*/ |
114
|
|
|
public function SetWatermark(Image $image) |
115
|
|
|
{ |
116
|
|
|
$this->_watermark = $image; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the watermark. |
122
|
|
|
* Throws an Exception if it's not set or if it's not an \library\image\Image |
123
|
|
|
* @return \library\images\Image |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
|
|
public function GetWatermark() |
127
|
|
|
{ |
128
|
|
|
if ($this->_watermark == null) throw new \Exception('A watermark is not set. Please supply a \library\image\Image using $this->SetWatermark'); |
129
|
|
|
return $this->_watermark; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the x |
134
|
|
|
* |
135
|
|
|
* @param int | string $x |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function SetX($x) |
139
|
|
|
{ |
140
|
|
|
$this->_x = $x; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the y |
146
|
|
|
* |
147
|
|
|
* @param int | string $y |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
|
|
public function SetY($y) |
151
|
|
|
{ |
152
|
|
|
$this->_y = $y; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function Execute($imageResource) |
157
|
|
|
{ |
158
|
|
|
$watermark = $this->GetWatermark(); |
159
|
|
|
$watermarkWidth = imagesx($watermark->GetImageResource()); |
160
|
|
|
$watermarkHeight = imagesy($watermark->GetImageResource()); |
161
|
|
|
|
162
|
|
|
$x = $this->calculateX($imageResource); |
163
|
|
|
$y = $this->calculateY($imageResource); |
164
|
|
|
|
165
|
|
|
$imageWidth = imagesx($imageResource); |
166
|
|
|
$imageHeight = imagesy($imageResource); |
167
|
|
|
|
168
|
|
|
$new = imagecreatetruecolor($imageWidth, $imageHeight); |
169
|
|
|
|
170
|
|
|
// Preserve transparency of the image |
171
|
|
|
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
172
|
|
|
imagealphablending($new, false); |
173
|
|
|
imagesavealpha($new, true); |
174
|
|
|
|
175
|
|
|
// Preserve transparency of the watermark |
176
|
|
|
imagecolortransparent($watermark->GetImageResource(), imagecolorallocatealpha($watermark->GetImageResource(), 0, 0, 0, 127)); |
177
|
|
|
imagealphablending($watermark->GetImageResource(), false); |
178
|
|
|
imagesavealpha($watermark->GetImageResource(), true); |
179
|
|
|
|
180
|
|
|
imagealphablending($new, true); |
181
|
|
|
imagealphablending($watermark->GetImageResource(), true); |
182
|
|
|
|
183
|
|
|
imagecopy($new, $imageResource, 0, 0, 0, 0, $imageWidth, $imageHeight); |
184
|
|
|
imagecopymerge($new, $watermark->GetImageResource(), $x, $y, 0, 0, $watermarkWidth, $watermarkHeight, $this->_transparency); |
185
|
|
|
|
186
|
|
|
return $new; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
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.