Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
Watermark.php
1
<?php
9
namespace
Bitrix\Main\File\Image
;
10
11
class
Watermark
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
26
$hAlign
=
self::ALIGN_RIGHT
,
27
$vAlign
=
self::ALIGN_BOTTOM
,
28
$size
=
self::SIZE_MEDIUM
,
29
$padding
= 0,
30
$ratio
;
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
{
63
$padding
=
$this->padding
;
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
}
Bitrix\Main\File\Image\Color\createFromHex
static createFromHex($color)
Definition
Color.php:39
Bitrix\Main\File\Image\ImageWatermark
Definition
ImageWatermark.php:12
Bitrix\Main\File\Image\Rectangle
Definition
Rectangle.php:14
Bitrix\Main\File\Image\Rectangle\setX
setX($x)
Definition
Rectangle.php:48
Bitrix\Main\File\Image\Rectangle\getX
getX()
Definition
Rectangle.php:39
Bitrix\Main\File\Image\Rectangle\setY
setY($y)
Definition
Rectangle.php:66
Bitrix\Main\File\Image\Rectangle\getHeight
getHeight()
Definition
Rectangle.php:93
Bitrix\Main\File\Image\Rectangle\getY
getY()
Definition
Rectangle.php:57
Bitrix\Main\File\Image\Rectangle\getWidth
getWidth()
Definition
Rectangle.php:75
Bitrix\Main\File\Image\TextWatermark
Definition
TextWatermark.php:14
Bitrix\Main\File\Image\Watermark
Definition
Watermark.php:12
Bitrix\Main\File\Image\Watermark\SIZE_MEDIUM
const SIZE_MEDIUM
Definition
Watermark.php:22
Bitrix\Main\File\Image\Watermark\__construct
__construct()
Definition
Watermark.php:32
Bitrix\Main\File\Image\Watermark\getPadding
getPadding()
Definition
Watermark.php:140
Bitrix\Main\File\Image\Watermark\getHorizontalAlignment
getHorizontalAlignment()
Definition
Watermark.php:132
Bitrix\Main\File\Image\Watermark\$ratio
$ratio
Definition
Watermark.php:30
Bitrix\Main\File\Image\Watermark\ALIGN_BOTTOM
const ALIGN_BOTTOM
Definition
Watermark.php:18
Bitrix\Main\File\Image\Watermark\$vAlign
$vAlign
Definition
Watermark.php:27
Bitrix\Main\File\Image\Watermark\createFromArray
static createFromArray($params)
Definition
Watermark.php:160
Bitrix\Main\File\Image\Watermark\alignPosition
alignPosition($width, $height, Rectangle $position)
Definition
Watermark.php:61
Bitrix\Main\File\Image\Watermark\getVerticalAlignment
getVerticalAlignment()
Definition
Watermark.php:124
Bitrix\Main\File\Image\Watermark\getRatio
getRatio()
Definition
Watermark.php:51
Bitrix\Main\File\Image\Watermark\ALIGN_TOP
const ALIGN_TOP
Definition
Watermark.php:17
Bitrix\Main\File\Image\Watermark\$padding
$padding
Definition
Watermark.php:29
Bitrix\Main\File\Image\Watermark\setAlignment
setAlignment($hAlign, $vAlign)
Definition
Watermark.php:41
Bitrix\Main\File\Image\Watermark\ALIGN_CENTER
const ALIGN_CENTER
Definition
Watermark.php:15
Bitrix\Main\File\Image\Watermark\ALIGN_RIGHT
const ALIGN_RIGHT
Definition
Watermark.php:16
Bitrix\Main\File\Image\Watermark\setSize
setSize($size)
Definition
Watermark.php:115
Bitrix\Main\File\Image\Watermark\setRatio
setRatio($ratio)
Definition
Watermark.php:105
Bitrix\Main\File\Image\Watermark\setPadding
setPadding($padding)
Definition
Watermark.php:149
Bitrix\Main\File\Image\Watermark\SIZE_SMALL
const SIZE_SMALL
Definition
Watermark.php:21
Bitrix\Main\File\Image\Watermark\$hAlign
$hAlign
Definition
Watermark.php:26
Bitrix\Main\File\Image\Watermark\ALIGN_LEFT
const ALIGN_LEFT
Definition
Watermark.php:14
Bitrix\Main\File\Image\Watermark\$size
$size
Definition
Watermark.php:28
Bitrix\Main\File\Image\Watermark\SIZE_BIG
const SIZE_BIG
Definition
Watermark.php:23
Bitrix\Main\File\Image
Definition
Color.php:9
modules
main
lib
File
Image
Watermark.php
Создано системой
1.10.0