PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / model / e2pdf-signature.php

e2pdf-signature.php in E2Pdf – Export Pdf Tool for WordPress 1.32.49, at classes/model/e2pdf-signature.php

359 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /model/e2pdf-signature.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Model_E2pdf_Signature extends Model_E2pdf_Model {
15
16 public function ttf_signature($value, $options) {
17
18 $response = '';
19
20 if ($value && trim($value) != '' && extension_loaded('gd') && function_exists('imagettftext')) {
21
22 $value = $this->helper->load('rtl')->rtl($value);
23
24 $box = imagettfbbox($options['fontSize'], 0, $options['font'], $value);
25
26 $min_x = min(array($box[0], $box[2], $box[4], $box[6]));
27 $max_x = max(array($box[0], $box[2], $box[4], $box[6]));
28 $min_y = min(array($box[1], $box[3], $box[5], $box[7]));
29 $max_y = max(array($box[1], $box[3], $box[5], $box[7]));
30
31 $box = array(
32 'x' => abs($min_x),
33 'y' => abs($min_y),
34 'width' => $max_x - $min_x,
35 'height' => $max_y - $min_y,
36 );
37
38 $box = $this->ttf_box_fix($value, $box, $options['fontSize'], $options['font']);
39
40 if ($box['width'] > 0 && $box['height'] > 0) {
41 $img = imagecreatetruecolor($box['width'], $box['height']);
42 if ($options['bgColour'] == 'transparent') {
43 imagealphablending($img, false);
44 imagesavealpha($img, true);
45 $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
46 } else {
47 $bg = imagecolorallocate($img, $options['bgColour'][0], $options['bgColour'][1], $options['bgColour'][2]);
48 }
49
50 $pen = imagecolorallocate($img, $options['penColour'][0], $options['penColour'][1], $options['penColour'][2]);
51 imagefill($img, 0, 0, $bg);
52 imagettftext($img, $options['fontSize'], 0, $box['x'], $box['y'], $pen, $options['font'], $value);
53 ob_start();
54 imagepng($img);
55 $tmp_image = ob_get_contents();
56 ob_end_clean();
57 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
58 $response = base64_encode($tmp_image);
59 }
60 }
61
62 return $response;
63 }
64
65 /*
66 * https://github.com/unlocomqx/text-measure
67 */
68
69 public function ttf_box_fix($value, $box, $size, $font) {
70
71 $img = $this->ttf_tmp($value, $box, $size, $font);
72
73 $top_line = true;
74 while ($top_line) {
75 $found = false;
76 $y = 1;
77 for ($x = 0; $x < $box['width']; $x++) {
78 $color = imagecolorat($img, $x, $y);
79 if ($color) {
80 $found = true;
81 break;
82 }
83 }
84 if (!$found) {
85 $top_line = false;
86 break;
87 } else {
88 $box['y']++;
89 $box['height']++;
90 imagedestroy($img);
91 $img = $this->ttf_tmp($value, $box, $size, $font);
92 }
93 }
94
95 $bottom_line = true;
96 while ($bottom_line) {
97 $found = false;
98 $y = $box['height'] - 1;
99 for ($x = 0; $x < $box['width']; $x++) {
100 $color = imagecolorat($img, $x, $y);
101 if ($color) {
102 $found = true;
103 break;
104 }
105 }
106 if (!$found) {
107 $bottom_line = false;
108 break;
109 } else {
110 $box['height']++;
111 imagedestroy($img);
112 $img = $this->ttf_tmp($value, $box, $size, $font);
113 }
114 }
115
116 $left_line = true;
117 while ($left_line) {
118 $found = false;
119 $x = 1;
120 for ($y = 0; $y < $box['height']; $y++) {
121 $color = imagecolorat($img, $x, $y);
122 if ($color) {
123 $found = true;
124 break;
125 }
126 }
127 if (!$found) {
128 $left_line = false;
129 break;
130 } else {
131 $box['x']++;
132 $box['width']++;
133 imagedestroy($img);
134 $img = $this->ttf_tmp($value, $box, $size, $font);
135 }
136 }
137
138 $right_line = true;
139 while ($right_line) {
140 $found = false;
141 $x = $box['width'] - 1;
142 for ($y = 0; $y < $box['height']; $y++) {
143 $color = imagecolorat($img, $x, $y);
144 if ($color) {
145 $found = true;
146 break;
147 }
148 }
149 if (!$found) {
150 $right_line = false;
151 break;
152 } else {
153 $box['width']++;
154 imagedestroy($img);
155 $img = $this->ttf_tmp($value, $box, $size, $font);
156 }
157 }
158
159 $found = false;
160 for ($y = 0; $y < $box['height']; $y++) {
161 for ($x = 0; $x < $box['width']; $x++) {
162 $color = imagecolorat($img, $x, $y);
163 if ($color) {
164 $found = true;
165 break;
166 }
167 }
168 if (!$found) {
169 $box['y']--;
170 $box['height']--;
171 } else {
172 break;
173 }
174 }
175
176 $found = false;
177 for ($y = $box['height'] - 1; $y >= 0; $y--) {
178 for ($x = 0; $x < $box['width']; $x++) {
179 $color = imagecolorat($img, $x, $y);
180 if ($color) {
181 $found = true;
182 break;
183 }
184 }
185 if (!$found) {
186 $box['height']--;
187 } else {
188 break;
189 }
190 }
191
192 $found = false;
193 for ($x = 0; $x < $box['width']; $x++) {
194 for ($y = 0; $y < $box['height']; $y++) {
195 $color = imagecolorat($img, $x, $y);
196 if ($color) {
197 $found = true;
198 break;
199 }
200 }
201 if (!$found) {
202 $box['x']--;
203 $box['width']--;
204 } else {
205 break;
206 }
207 }
208
209 $found = false;
210 for ($x = $box['width'] - 1; $x >= 0; $x--) {
211 for ($y = 0; $y < $box['height']; $y++) {
212 $color = imagecolorat($img, $x, $y);
213 if ($color) {
214 $found = true;
215 break;
216 }
217 }
218 if (!$found) {
219 $box['width']--;
220 } else {
221 break;
222 }
223 }
224 return $box;
225 }
226
227 public function ttf_tmp($value, $box, $size, $font) {
228 $img = imagecreatetruecolor($box['width'], $box['height']);
229 $black = imagecolorallocate($img, 0, 0, 0);
230 $red = imagecolorallocate($img, 255, 0, 0);
231 imagefill($img, 0, 0, $black);
232 imagettftext($img, $size, 0, (int) $box['x'], $box['y'], $red, $font, $value);
233 imagecolordeallocate($img, $black);
234 imagecolordeallocate($img, $red);
235 return $img;
236 }
237
238 public function j_signature($value, $options = array()) {
239
240 $response = '';
241
242 $value = str_replace('image/jsignature;base30,', '', $value);
243 $a = $this->helper->load('jSignature')->Base64ToNative($value);
244
245 $width = 0;
246 $height = 0;
247
248 foreach ($a as $line) {
249 if (max($line ['x']) > $width) {
250 $width = max($line ['x']);
251 }
252 if (max($line ['y']) > $height) {
253 $height = max($line ['y']);
254 }
255 }
256
257 $img = imagecreatetruecolor($width, $height);
258
259 if ($options['bgColour'] == 'transparent') {
260 imagealphablending($img, false);
261 imagesavealpha($img, true);
262 $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
263 } else {
264 $bg = imagecolorallocate($img, $options['bgColour'][0], $options['bgColour'][1], $options['bgColour'][2]);
265 }
266
267 imagefill($img, 0, 0, $bg);
268 imagesetthickness($img, 5);
269
270 $pen = imagecolorallocate($img, $options['penColour'][0], $options['penColour'][1], $options['penColour'][2]);
271
272 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed, Squiz.PHP.DisallowSizeFunctionsInLoops.Found
273 for ($i = 0; $i < count($a); $i++) {
274 // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed, Squiz.PHP.DisallowSizeFunctionsInLoops.Found
275 for ($j = 0; $j < count($a[$i]['x']); $j++) {
276 if (!isset($a[$i]['x'][$j])) {
277 break;
278 }
279 if (!isset($a[$i]['x'][$j + 1])) {
280 imagesetpixel($img, $a[$i]['x'][$j], $a[$i]['y'][$j], $pen);
281 } else {
282 imageline($img, $a[$i]['x'][$j], $a[$i]['y'][$j], $a[$i]['x'][$j + 1], $a[$i]['y'][$j + 1], $pen);
283 }
284 }
285 }
286
287 $tmp_image = $this->j_signature_trimbox($img);
288 imagedestroy($img);
289
290 if ($tmp_image) {
291 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
292 $response = base64_encode($tmp_image);
293 }
294
295 return $response;
296 }
297
298 public function j_signature_trimbox($img = false) {
299 if (!$img) {
300 return '';
301 }
302
303 if (is_string($img)) {
304 $img = imagecreatefrompng($img);
305 }
306
307 $width = imagesx($img);
308 $height = imagesy($img);
309
310 $top = 0;
311 $bottom = 0;
312 $left = 0;
313 $right = 0;
314
315 $bgcolor = imagecolorat($img, $top, $left);
316 for (; $top < $height; ++$top) {
317 for ($x = 0; $x < $width; ++$x) {
318 if (imagecolorat($img, $x, $top) != $bgcolor) {
319 break 2;
320 }
321 }
322 }
323
324 for (; $bottom < $height; ++$bottom) {
325 for ($x = 0; $x < $width; ++$x) {
326 if (imagecolorat($img, $x, $height - $bottom - 1) != $bgcolor) {
327 break 2;
328 }
329 }
330 }
331
332 for (; $left < $width; ++$left) {
333 for ($y = 0; $y < $height; ++$y) {
334 if (imagecolorat($img, $left, $y) != $bgcolor) {
335 break 2;
336 }
337 }
338 }
339
340 for (; $right < $width; ++$right) {
341 for ($y = 0; $y < $height; ++$y) {
342 if (imagecolorat($img, $width - $right - 1, $y) != $bgcolor) {
343 break 2;
344 }
345 }
346 }
347
348 $newimg = imagecreate($width - ($left + $right), $height - ($top + $bottom));
349 imagecopy($newimg, $img, 0, 0, $left, $top, imagesx($newimg), imagesy($newimg));
350
351 ob_start();
352 imagepng($newimg);
353 $imagedata = ob_get_contents();
354 ob_end_clean();
355
356 return $imagedata;
357 }
358 }
359