PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.53
E2Pdf – Export Pdf Tool for WordPress v1.32.53
1.32.53 1.32.51 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 All 74 releases
← All changes | classes/model/e2pdf-signature.php +177 -62 1.05.03 → 1.32.53 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Signature Model
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 1.00.10
4 + * File: /model/e2pdf-signature.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -14,45 +12,55 @@
14 12 }
15 13
16 14 class Model_E2pdf_Signature extends Model_E2pdf_Model {
17 15
18 - public function ttf_signature($value, $size, $font, $options) {
16 + public function ttf_signature($value, $options) {
19 17
20 - $box = imagettfbbox($size, 0, $font, $value);
18 + $response = '';
21 19
22 - $min_x = min(array($box[0], $box[2], $box[4], $box[6]));
23 - $max_x = max(array($box[0], $box[2], $box[4], $box[6]));
24 - $min_y = min(array($box[1], $box[3], $box[5], $box[7]));
25 - $max_y = max(array($box[1], $box[3], $box[5], $box[7]));
20 + if ($value && trim($value) != '' && extension_loaded('gd') && function_exists('imagettftext')) {
26 21
27 - $box = array(
28 - 'x' => abs($min_x),
29 - 'y' => abs($min_y),
30 - 'width' => $max_x - $min_x,
31 - 'height' => $max_y - $min_y
32 - );
22 + $value = $this->helper->load('rtl')->rtl($value);
33 23
34 - $box = $this->ttf_box_fix($value, $box, $size, $font);
24 + $box = imagettfbbox($options['fontSize'], 0, $options['font'], $value);
35 25
36 - $img = imagecreatetruecolor($box['width'], $box['height']);
37 - if ($options['bgColour'] == 'transparent') {
38 - imagealphablending($img, false);
39 - imagesavealpha($img, true);
40 - $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
41 - } else {
42 - $bg = imagecolorallocate($img, $options['bgColour'][0], $options['bgColour'][1], $options['bgColour'][2]);
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 + }
43 60 }
44 61
45 - $pen = imagecolorallocate($img, $options['penColour'][0], $options['penColour'][1], $options['penColour'][2]);
46 - imagefill($img, 0, 0, $bg);
47 - imagettftext($img, $size, 0, $box['x'], $box['y'], $pen, $font, $value);
48 - ob_start();
49 - imagepng($img);
50 - $tmp_image = ob_get_contents();
51 - ob_end_clean();
52 - $value = base64_encode($tmp_image);
53 -
54 - return $value;
62 + return $response;
55 63 }
56 64
57 65 /*
58 66 * https://github.com/unlocomqx/text-measure
@@ -61,9 +69,8 @@
61 69 public function ttf_box_fix($value, $box, $size, $font) {
62 70
63 71 $img = $this->ttf_tmp($value, $box, $size, $font);
64 72
65 - //top add
66 73 $top_line = true;
67 74 while ($top_line) {
68 75 $found = false;
69 76 $y = 1;
@@ -77,17 +84,15 @@
77 84 if (!$found) {
78 85 $top_line = false;
79 86 break;
80 87 } else {
81 - $box['y'] ++;
82 - $box['height'] ++;
88 + $box['y']++;
89 + $box['height']++;
83 90 imagedestroy($img);
84 91 $img = $this->ttf_tmp($value, $box, $size, $font);
85 92 }
86 93 }
87 94
88 -
89 - //bottom add
90 95 $bottom_line = true;
91 96 while ($bottom_line) {
92 97 $found = false;
93 98 $y = $box['height'] - 1;
@@ -101,15 +106,14 @@
101 106 if (!$found) {
102 107 $bottom_line = false;
103 108 break;
104 109 } else {
105 - $box['height'] ++;
110 + $box['height']++;
106 111 imagedestroy($img);
107 112 $img = $this->ttf_tmp($value, $box, $size, $font);
108 113 }
109 114 }
110 115
111 - //left add
112 116 $left_line = true;
113 117 while ($left_line) {
114 118 $found = false;
115 119 $x = 1;
@@ -123,16 +127,15 @@
123 127 if (!$found) {
124 128 $left_line = false;
125 129 break;
126 130 } else {
127 - $box['x'] ++;
128 - $box['width'] ++;
131 + $box['x']++;
132 + $box['width']++;
129 133 imagedestroy($img);
130 134 $img = $this->ttf_tmp($value, $box, $size, $font);
131 135 }
132 136 }
133 137
134 - //right add
135 138 $right_line = true;
136 139 while ($right_line) {
137 140 $found = false;
138 141 $x = $box['width'] - 1;
@@ -146,15 +149,14 @@
146 149 if (!$found) {
147 150 $right_line = false;
148 151 break;
149 152 } else {
150 - $box['width'] ++;
153 + $box['width']++;
151 154 imagedestroy($img);
152 155 $img = $this->ttf_tmp($value, $box, $size, $font);
153 156 }
154 157 }
155 158
156 - //top trim
157 159 $found = false;
158 160 for ($y = 0; $y < $box['height']; $y++) {
159 161 for ($x = 0; $x < $box['width']; $x++) {
160 162 $color = imagecolorat($img, $x, $y);
@@ -163,17 +165,15 @@
163 165 break;
164 166 }
165 167 }
166 168 if (!$found) {
167 - $box['y'] --;
168 - $box['height'] --;
169 + $box['y']--;
170 + $box['height']--;
169 171 } else {
170 172 break;
171 173 }
172 174 }
173 175
174 -
175 - //bottom trim
176 176 $found = false;
177 177 for ($y = $box['height'] - 1; $y >= 0; $y--) {
178 178 for ($x = 0; $x < $box['width']; $x++) {
179 179 $color = imagecolorat($img, $x, $y);
@@ -182,17 +182,14 @@
182 182 break;
183 183 }
184 184 }
185 185 if (!$found) {
186 - $box['height'] --;
186 + $box['height']--;
187 187 } else {
188 188 break;
189 189 }
190 190 }
191 191
192 -
193 -
194 - //left trim
195 192 $found = false;
196 193 for ($x = 0; $x < $box['width']; $x++) {
197 194 for ($y = 0; $y < $box['height']; $y++) {
198 195 $color = imagecolorat($img, $x, $y);
@@ -201,17 +198,15 @@
201 198 break;
202 199 }
203 200 }
204 201 if (!$found) {
205 - $box['x'] --;
206 - $box['width'] --;
202 + $box['x']--;
203 + $box['width']--;
207 204 } else {
208 205 break;
209 206 }
210 207 }
211 208
212 -
213 - //right trim
214 209 $found = false;
215 210 for ($x = $box['width'] - 1; $x >= 0; $x--) {
216 211 for ($y = 0; $y < $box['height']; $y++) {
217 212 $color = imagecolorat($img, $x, $y);
@@ -220,9 +215,9 @@
220 215 break;
221 216 }
222 217 }
223 218 if (!$found) {
224 - $box['width'] --;
219 + $box['width']--;
225 220 } else {
226 221 break;
227 222 }
228 223 }
@@ -239,5 +234,125 @@
239 234 imagecolordeallocate($img, $red);
240 235 return $img;
241 236 }
242 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 + }
243 358 }