1 | <?php |
||
2 | |||
3 | namespace Jackal\ImageMerge\Model\Text; |
||
4 | |||
5 | use Exception; |
||
6 | use Jackal\ImageMerge\Model\Color; |
||
7 | use Jackal\ImageMerge\Model\Font\Font; |
||
8 | |||
9 | /** |
||
10 | * Class Text |
||
11 | * @package Jackal\ImageMerge\Model\Text |
||
12 | */ |
||
13 | class Text |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $text; |
||
19 | |||
20 | /** |
||
21 | * @var Font |
||
22 | */ |
||
23 | private $font; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $size; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $color; |
||
34 | |||
35 | /** |
||
36 | * Text constructor. |
||
37 | * @param $text |
||
38 | * @param Font $font |
||
39 | * @param $size |
||
40 | * @param $color |
||
41 | */ |
||
42 | public function __construct($text, Font $font, $size, Color $color) |
||
43 | { |
||
44 | $this->text = $text; |
||
45 | $this->font = $font; |
||
46 | $this->size = $size; |
||
47 | $this->color = $color; |
||
0 ignored issues
–
show
|
|||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param null $boxWidth |
||
0 ignored issues
–
show
|
|||
52 | * @param null $boxHeight |
||
0 ignored issues
–
show
|
|||
53 | * @return Text |
||
54 | * @throws Exception |
||
55 | */ |
||
56 | public function fitToBox($boxWidth = null, $boxHeight = null) |
||
57 | { |
||
58 | if (is_null($boxWidth) and is_null($boxHeight)) { |
||
0 ignored issues
–
show
|
|||
59 | throw new Exception('At least one dimension must be defined'); |
||
60 | } |
||
61 | |||
62 | $finalSize = 1; |
||
63 | for ($i = 1;$i <= 1000;$i = $i + 0.5) { |
||
64 | $textbox = imageftbbox(round($this->fontToPixel($i)), 0, (string) $this->getFont(), $this->getText()); |
||
65 | |||
66 | $height = $textbox[1] + abs($textbox[7]); |
||
67 | $width = abs($textbox[2]) + $textbox[0]; |
||
68 | if ((($height < $boxHeight) or is_null($boxHeight)) and (($width < $boxWidth) or is_null($boxWidth))) { |
||
69 | continue; |
||
70 | } |
||
71 | $finalSize = $i; |
||
72 | |||
73 | break; |
||
74 | } |
||
75 | $this->size = $finalSize; |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getWidth() |
||
84 | { |
||
85 | return $this->getBoundBox()['width']; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return int |
||
90 | */ |
||
91 | public function getHeight() |
||
92 | { |
||
93 | return $this->getBoundBox()['height']; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | private function getBoundBox() |
||
100 | { |
||
101 | $textbox = imageftbbox($this->fontToPixel($this->size), 0, (string) $this->getFont(), $this->getText()); |
||
102 | |||
103 | return [ |
||
104 | 'width' => abs($textbox[2]) + $textbox[0], |
||
105 | 'height' => $textbox[1] + abs($textbox[7]), |
||
106 | ]; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $size |
||
111 | * @return float |
||
112 | */ |
||
113 | private function fontToPixel($size) |
||
114 | { |
||
115 | return round($size * 0.75); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getText() |
||
122 | { |
||
123 | return $this->text; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return Font |
||
128 | */ |
||
129 | public function getFont() |
||
130 | { |
||
131 | return $this->font; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return integer |
||
136 | */ |
||
137 | public function getFontSize() |
||
138 | { |
||
139 | return $this->size; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return Color |
||
144 | */ |
||
145 | public function getColor() |
||
146 | { |
||
147 | return $this->color; |
||
0 ignored issues
–
show
|
|||
148 | } |
||
149 | } |
||
150 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..