PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
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.08.06, at classes/model/e2pdf-signature.php

248 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
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
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Model_E2pdf_Signature extends Model_E2pdf_Model {
17
18 public function ttf_signature($value, $size, $font, $options) {
19
20 $box = imagettfbbox($size, 0, $font, $value);
21
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]));
26
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 );
33
34 $box = $this->ttf_box_fix($value, $box, $size, $font);
35
36 if ($box['width'] > 0 && $box['height'] > 0) {
37 $img = imagecreatetruecolor($box['width'], $box['height']);
38 if ($options['bgColour'] == 'transparent') {
39 imagealphablending($img, false);
40 imagesavealpha($img, true);
41 $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
42 } else {
43 $bg = imagecolorallocate($img, $options['bgColour'][0], $options['bgColour'][1], $options['bgColour'][2]);
44 }
45
46 $pen = imagecolorallocate($img, $options['penColour'][0], $options['penColour'][1], $options['penColour'][2]);
47 imagefill($img, 0, 0, $bg);
48 imagettftext($img, $size, 0, $box['x'], $box['y'], $pen, $font, $value);
49 ob_start();
50 imagepng($img);
51 $tmp_image = ob_get_contents();
52 ob_end_clean();
53 $value = base64_encode($tmp_image);
54 } else {
55 $value = '';
56 }
57
58 return $value;
59 }
60
61 /*
62 * https://github.com/unlocomqx/text-measure
63 */
64
65 public function ttf_box_fix($value, $box, $size, $font) {
66
67 $img = $this->ttf_tmp($value, $box, $size, $font);
68
69 //top add
70 $top_line = true;
71 while ($top_line) {
72 $found = false;
73 $y = 1;
74 for ($x = 0; $x < $box['width']; $x++) {
75 $color = imagecolorat($img, $x, $y);
76 if ($color) {
77 $found = true;
78 break;
79 }
80 }
81 if (!$found) {
82 $top_line = false;
83 break;
84 } else {
85 $box['y'] ++;
86 $box['height'] ++;
87 imagedestroy($img);
88 $img = $this->ttf_tmp($value, $box, $size, $font);
89 }
90 }
91
92
93 //bottom add
94 $bottom_line = true;
95 while ($bottom_line) {
96 $found = false;
97 $y = $box['height'] - 1;
98 for ($x = 0; $x < $box['width']; $x++) {
99 $color = imagecolorat($img, $x, $y);
100 if ($color) {
101 $found = true;
102 break;
103 }
104 }
105 if (!$found) {
106 $bottom_line = false;
107 break;
108 } else {
109 $box['height'] ++;
110 imagedestroy($img);
111 $img = $this->ttf_tmp($value, $box, $size, $font);
112 }
113 }
114
115 //left add
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 add
139 $right_line = true;
140 while ($right_line) {
141 $found = false;
142 $x = $box['width'] - 1;
143 for ($y = 0; $y < $box['height']; $y++) {
144 $color = imagecolorat($img, $x, $y);
145 if ($color) {
146 $found = true;
147 break;
148 }
149 }
150 if (!$found) {
151 $right_line = false;
152 break;
153 } else {
154 $box['width'] ++;
155 imagedestroy($img);
156 $img = $this->ttf_tmp($value, $box, $size, $font);
157 }
158 }
159
160 //top trim
161 $found = false;
162 for ($y = 0; $y < $box['height']; $y++) {
163 for ($x = 0; $x < $box['width']; $x++) {
164 $color = imagecolorat($img, $x, $y);
165 if ($color) {
166 $found = true;
167 break;
168 }
169 }
170 if (!$found) {
171 $box['y'] --;
172 $box['height'] --;
173 } else {
174 break;
175 }
176 }
177
178
179 //bottom trim
180 $found = false;
181 for ($y = $box['height'] - 1; $y >= 0; $y--) {
182 for ($x = 0; $x < $box['width']; $x++) {
183 $color = imagecolorat($img, $x, $y);
184 if ($color) {
185 $found = true;
186 break;
187 }
188 }
189 if (!$found) {
190 $box['height'] --;
191 } else {
192 break;
193 }
194 }
195
196
197
198 //left trim
199 $found = false;
200 for ($x = 0; $x < $box['width']; $x++) {
201 for ($y = 0; $y < $box['height']; $y++) {
202 $color = imagecolorat($img, $x, $y);
203 if ($color) {
204 $found = true;
205 break;
206 }
207 }
208 if (!$found) {
209 $box['x'] --;
210 $box['width'] --;
211 } else {
212 break;
213 }
214 }
215
216
217 //right trim
218 $found = false;
219 for ($x = $box['width'] - 1; $x >= 0; $x--) {
220 for ($y = 0; $y < $box['height']; $y++) {
221 $color = imagecolorat($img, $x, $y);
222 if ($color) {
223 $found = true;
224 break;
225 }
226 }
227 if (!$found) {
228 $box['width'] --;
229 } else {
230 break;
231 }
232 }
233 return $box;
234 }
235
236 public function ttf_tmp($value, $box, $size, $font) {
237 $img = imagecreatetruecolor($box['width'], $box['height']);
238 $black = imagecolorallocate($img, 0, 0, 0);
239 $red = imagecolorallocate($img, 255, 0, 0);
240 imagefill($img, 0, 0, $black);
241 imagettftext($img, $size, 0, (int) $box['x'], $box['y'], $red, $font, $value);
242 imagecolordeallocate($img, $black);
243 imagecolordeallocate($img, $red);
244 return $img;
245 }
246
247 }
248