1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* |
6
|
|
|
* @package MyArtJaub\Webtrees |
7
|
|
|
* @subpackage Sosa |
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2020-2022, Jonathan Jaubart |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\Certificates\Model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Model class for watermark to be applied to a certificate image. |
19
|
|
|
*/ |
20
|
|
|
class Watermark |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Default font color for watermarks |
24
|
|
|
* @var string DEFAULT_COLOR |
25
|
|
|
* */ |
26
|
|
|
public const DEFAULT_COLOR = '#4D6DF3'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Default maximum font size for watermarks |
30
|
|
|
* @var int DEFAULT_SIZE |
31
|
|
|
* */ |
32
|
|
|
public const DEFAULT_SIZE = 18; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string $text |
36
|
|
|
*/ |
37
|
|
|
private $text; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string $color; |
41
|
|
|
*/ |
42
|
|
|
private $color; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int $size |
47
|
|
|
*/ |
48
|
|
|
private $size; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor for Watermark data class |
52
|
|
|
* |
53
|
|
|
* @param string $text |
54
|
|
|
* @param string $color |
55
|
|
|
* @param int $size |
56
|
|
|
*/ |
57
|
|
|
public function __construct(string $text, string $color, int $size) |
58
|
|
|
{ |
59
|
|
|
$this->text = $text; |
60
|
|
|
$this->color = $color; |
61
|
|
|
$this->size = $size; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the watermark text. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function text(): string |
70
|
|
|
{ |
71
|
|
|
return $this->text; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the watermark font color. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function color(): string |
80
|
|
|
{ |
81
|
|
|
return $this->color; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the watermark maximum font size. |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function size(): int |
89
|
|
|
{ |
90
|
|
|
return $this->size; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return an estimate of the size in pixels of the watermark text length. |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function textLengthEstimate(): int |
99
|
|
|
{ |
100
|
|
|
return $this->stringLengthEstimate(mb_strlen($this->text), $this->size); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Decrease the font size if necessary, based on the image width. |
105
|
|
|
* |
106
|
|
|
* @param int $width |
107
|
|
|
*/ |
108
|
|
|
public function adjustSize(int $width): void |
109
|
|
|
{ |
110
|
|
|
$len = mb_strlen($this->text); |
111
|
|
|
while ($this->stringLengthEstimate($len, $this->size) > 0.9 * $width) { |
112
|
|
|
$this->size--; |
113
|
|
|
if ($this->size === 2) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return an estimate of the size in pixels of a text in a specified font size. |
121
|
|
|
* |
122
|
|
|
* @param int $text_length |
123
|
|
|
* @param int $font_size |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
private function stringLengthEstimate(int $text_length, int $font_size): int |
127
|
|
|
{ |
128
|
|
|
return $text_length * (int) ceil(($font_size + 2) * 0.5); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|