|
1
|
|
|
<?php |
|
2
|
|
|
namespace tinymeng\code\Gateways\barcode; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
*-------------------------------------------------------------------- |
|
6
|
|
|
* |
|
7
|
|
|
* Holds font family and size. |
|
8
|
|
|
* |
|
9
|
|
|
*-------------------------------------------------------------------- |
|
10
|
|
|
* Copyright (C) Jean-Sebastien Goupil |
|
11
|
|
|
* http://www.barcodephp.com |
|
12
|
|
|
*/ |
|
13
|
|
|
class BCGFontFile implements BCGFont { |
|
14
|
|
|
const PHP_BOX_FIX = 0; |
|
15
|
|
|
|
|
16
|
|
|
private $path; |
|
17
|
|
|
private $size; |
|
18
|
|
|
private $text = ''; |
|
19
|
|
|
private $foregroundColor; |
|
20
|
|
|
private $rotationAngle; |
|
21
|
|
|
private $box; |
|
22
|
|
|
private $boxFix; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $fontPath path to the file |
|
28
|
|
|
* @param int $size size in point |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($fontPath, $size) { |
|
31
|
|
|
if (!file_exists($fontPath)) { |
|
32
|
|
|
throw new BCGArgumentException('The font path is incorrect.', 'fontPath'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->path = $fontPath; |
|
36
|
|
|
$this->size = $size; |
|
37
|
|
|
$this->foregroundColor = new BCGColor('black'); |
|
38
|
|
|
$this->setRotationAngle(0); |
|
39
|
|
|
$this->setBoxFix(self::PHP_BOX_FIX); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gets the text associated to the font. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getText() { |
|
48
|
|
|
return $this->text; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sets the text associated to the font. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string text |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
|
|
public function setText($text) { |
|
57
|
|
|
$this->text = $text; |
|
58
|
|
|
$this->box = null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets the rotation in degree. |
|
63
|
|
|
* |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getRotationAngle() { |
|
67
|
|
|
return (360 - $this->rotationAngle) % 360; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets the rotation in degree. |
|
72
|
|
|
* |
|
73
|
|
|
* @param int |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setRotationAngle($rotationAngle) { |
|
76
|
|
|
$this->rotationAngle = (int)$rotationAngle; |
|
77
|
|
|
if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) { |
|
78
|
|
|
$this->rotationAngle = 0; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->rotationAngle = (360 - $this->rotationAngle) % 360; |
|
82
|
|
|
|
|
83
|
|
|
$this->box = null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the background color. |
|
88
|
|
|
* |
|
89
|
|
|
* @return BCGColor |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getBackgroundColor() { |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets the background color. |
|
96
|
|
|
* |
|
97
|
|
|
* @param BCGColor $backgroundColor |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setBackgroundColor($backgroundColor) { |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the foreground color. |
|
104
|
|
|
* |
|
105
|
|
|
* @return BCGColor |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getForegroundColor() { |
|
108
|
|
|
return $this->foregroundColor; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Sets the foreground color. |
|
113
|
|
|
* |
|
114
|
|
|
* @param BCGColor $foregroundColor |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setForegroundColor($foregroundColor) { |
|
117
|
|
|
$this->foregroundColor = $foregroundColor; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets the box fix information. |
|
122
|
|
|
* |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getBoxFix() { |
|
126
|
|
|
return $this->boxFix; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sets the box fix information. |
|
131
|
|
|
* |
|
132
|
|
|
* @param int $value |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setBoxFix($value) { |
|
135
|
|
|
$this->boxFix = intval($value); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns the width and height that the text takes to be written. |
|
140
|
|
|
* |
|
141
|
|
|
* @return int[] |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getDimension() { |
|
144
|
|
|
$w = 0.0; |
|
145
|
|
|
$h = 0.0; |
|
146
|
|
|
$box = $this->getBox(); |
|
147
|
|
|
|
|
148
|
|
|
if ($box !== null) { |
|
149
|
|
|
$minX = min(array($box[0], $box[2], $box[4], $box[6])); |
|
150
|
|
|
$maxX = max(array($box[0], $box[2], $box[4], $box[6])); |
|
151
|
|
|
$minY = min(array($box[1], $box[3], $box[5], $box[7])); |
|
152
|
|
|
$maxY = max(array($box[1], $box[3], $box[5], $box[7])); |
|
153
|
|
|
|
|
154
|
|
|
$w = $maxX - $minX; |
|
155
|
|
|
$h = $maxY - $minY; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$rotationAngle = $this->getRotationAngle(); |
|
159
|
|
|
if ($rotationAngle === 90 || $rotationAngle === 270) { |
|
160
|
|
|
return array($h + self::PHP_BOX_FIX, $w); |
|
161
|
|
|
} else { |
|
162
|
|
|
return array($w + self::PHP_BOX_FIX, $h); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Draws the text on the image at a specific position. |
|
168
|
|
|
* $x and $y represent the left bottom corner. |
|
169
|
|
|
* |
|
170
|
|
|
* @param resource $im |
|
171
|
|
|
* @param int $x |
|
172
|
|
|
* @param int $y |
|
173
|
|
|
*/ |
|
174
|
|
|
public function draw($im, $x, $y) { |
|
175
|
|
|
$drawingPosition = $this->getDrawingPosition($x, $y); |
|
176
|
|
|
imagettftext($im, $this->size, $this->rotationAngle, $drawingPosition[0], $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function getDrawingPosition($x, $y) { |
|
180
|
|
|
$dimension = $this->getDimension(); |
|
181
|
|
|
$box = $this->getBox(); |
|
182
|
|
|
$rotationAngle = $this->getRotationAngle(); |
|
183
|
|
|
if ($rotationAngle === 0) { |
|
184
|
|
|
$y += abs(min($box[5], $box[7])); |
|
185
|
|
|
} elseif ($rotationAngle === 90) { |
|
186
|
|
|
$x += abs(min($box[5], $box[7])); |
|
187
|
|
|
$y += $dimension[1]; |
|
188
|
|
|
} elseif ($rotationAngle === 180) { |
|
189
|
|
|
$x += $dimension[0]; |
|
190
|
|
|
$y += abs(max($box[1], $box[3])); |
|
191
|
|
|
} elseif ($rotationAngle === 270) { |
|
192
|
|
|
$x += abs(max($box[1], $box[3])); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return array($x, $y); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
private function getBox() { |
|
199
|
|
|
if ($this->box === null) { |
|
200
|
|
|
$gd = imagecreate(1, 1); |
|
201
|
|
|
$this->box = imagettftext($gd, $this->size, 0, 0, 0, 0, $this->path, $this->text); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $this->box; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
?> |
|
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths