Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Watermark.php
1<?php
10
12{
13 const
14 ALIGN_LEFT = "left",
15 ALIGN_CENTER = "center",
16 ALIGN_RIGHT = "right",
17 ALIGN_TOP = "top",
18 ALIGN_BOTTOM = "bottom";
19
20 const
21 SIZE_SMALL = "small",
22 SIZE_MEDIUM = "medium",
23 SIZE_BIG = "big";
24
25 protected
31
32 public function __construct()
33 {
34 }
35
41 public function setAlignment($hAlign, $vAlign)
42 {
43 $this->hAlign = $hAlign;
44 $this->vAlign = $vAlign;
45 return $this;
46 }
47
51 public function getRatio()
52 {
53 return $this->ratio;
54 }
55
61 public function alignPosition($width, $height, Rectangle $position)
62 {
64
65 if($this->vAlign == static::ALIGN_CENTER)
66 {
67 $position->setY(($height - $position->getHeight()) / 2);
68 }
69 elseif($this->vAlign == static::ALIGN_BOTTOM)
70 {
71 $position->setY($height - $position->getHeight() - $padding);
72 }
73 else //static::ALIGN_TOP
74 {
75 $position->setY($padding);
76 }
77
78 if($this->hAlign == static::ALIGN_CENTER)
79 {
80 $position->setX(($width - $position->getWidth()) / 2);
81 }
82 elseif($this->hAlign == static::ALIGN_RIGHT)
83 {
84 $position->setX(($width - $position->getWidth()) - $padding);
85 }
86 else //static::ALIGN_LEFT
87 {
88 $position->setX($padding);
89 }
90
91 if($position->getY() < $padding)
92 {
93 $position->setY($padding);
94 }
95 if($position->getX() < $padding)
96 {
97 $position->setX($padding);
98 }
99 }
100
105 public function setRatio($ratio)
106 {
107 $this->ratio = (float)$ratio;
108 return $this;
109 }
110
115 public function setSize($size)
116 {
117 $this->size = $size;
118 return $this;
119 }
120
124 public function getVerticalAlignment()
125 {
126 return $this->vAlign;
127 }
128
132 public function getHorizontalAlignment()
133 {
134 return $this->hAlign;
135 }
136
140 public function getPadding()
141 {
142 return $this->padding;
143 }
144
149 public function setPadding($padding)
150 {
151 $this->padding = (int)$padding;
152 return $this;
153 }
154
160 public static function createFromArray($params)
161 {
162 $params["position"] = strtolower(trim($params["position"] ?? ''));
163 $positions = ["topleft", "topcenter", "topright", "centerleft", "center", "centerright", "bottomleft", "bottomcenter", "bottomright"];
164 $shortPositions = ["tl", "tc", "tr", "ml", "mc", "mr", "bl", "bc", "br"];
165 $position = ['x' => 'right','y' => 'bottom']; // Default position
166
167 if(in_array($params["position"], $shortPositions))
168 {
169 $params["position"] = str_replace($shortPositions, $positions, $params["position"]);
170 }
171
172 if(in_array($params["position"], $positions))
173 {
174 foreach(['top', 'center', 'bottom'] as $k)
175 {
176 $l = strlen($k);
177 if(substr($params["position"], 0, $l) == $k)
178 {
179 $position['y'] = $k;
180 $position['x'] = substr($params["position"], $l);
181 if($position['x'] == '')
182 {
183 $position['x'] = ($k == 'center'? 'center' : 'right');
184 }
185 }
186 }
187 }
188
189 if(isset($params["type"]) && $params["type"] == "text")
190 {
191 $watermark = new TextWatermark(
192 $params['text'],
193 $params['font'],
194 Color::createFromHex($params['color'])
195 );
196
197 if($params["text_width"] > 0)
198 {
199 $watermark->setWidth($params["text_width"]);
200 }
201 if($params["use_copyright"] == "Y")
202 {
203 $watermark->setCopyright(true);
204 }
205 }
206 else
207 {
208 $watermark = new ImageWatermark($params["file"] ?? null);
209
210 if(!isset($params["fill"]) || $params["fill"] <> 'repeat')
211 {
212 if(!isset($params["fill"]) || $params["fill"] <> 'exact')
213 {
214 if(isset($params["size"]) && $params["size"] == "real")
215 {
216 $params["fill"] = 'exact';
217 }
218 else
219 {
220 $params["fill"] = 'resize';
221 }
222 }
223 }
224 else
225 {
226 $position["x"] = "left";
227 $position["y"] = "top";
228 }
229
230 $watermark->setMode($params["fill"]);
231
232 if(isset($params["alpha_level"]))
233 {
234 $watermark->setAlpha(intval($params["alpha_level"]) / 100);
235 }
236 }
237
238 $watermark->setAlignment($position["x"], $position["y"]);
239
240 if(isset($params["padding"]))
241 {
242 $watermark->setPadding($params["padding"]);
243 }
244
245 if(isset($params["size"]))
246 {
247 $watermark->setSize($params["size"]);
248 }
249
250 if(isset($params["coefficient"]))
251 {
252 $watermark->setRatio($params["coefficient"]);
253 }
254
255 return $watermark;
256 }
257}
static createFromHex($color)
Definition Color.php:39
alignPosition($width, $height, Rectangle $position)
Definition Watermark.php:61
setAlignment($hAlign, $vAlign)
Definition Watermark.php:41