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

244 lines 6.8 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 $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]);
43 }
44
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;
55 }
56
57 /*
58 * https://github.com/unlocomqx/text-measure
59 */
60
61 public function ttf_box_fix($value, $box, $size, $font) {
62
63 $img = $this->ttf_tmp($value, $box, $size, $font);
64
65 //top add
66 $top_line = true;
67 while ($top_line) {
68 $found = false;
69 $y = 1;
70 for ($x = 0; $x < $box['width']; $x++) {
71 $color = imagecolorat($img, $x, $y);
72 if ($color) {
73 $found = true;
74 break;
75 }
76 }
77 if (!$found) {
78 $top_line = false;
79 break;
80 } else {
81 $box['y'] ++;
82 $box['height'] ++;
83 imagedestroy($img);
84 $img = $this->ttf_tmp($value, $box, $size, $font);
85 }
86 }
87
88
89 //bottom add
90 $bottom_line = true;
91 while ($bottom_line) {
92 $found = false;
93 $y = $box['height'] - 1;
94 for ($x = 0; $x < $box['width']; $x++) {
95 $color = imagecolorat($img, $x, $y);
96 if ($color) {
97 $found = true;
98 break;
99 }
100 }
101 if (!$found) {
102 $bottom_line = false;
103 break;
104 } else {
105 $box['height'] ++;
106 imagedestroy($img);
107 $img = $this->ttf_tmp($value, $box, $size, $font);
108 }
109 }
110
111 //left add
112 $left_line = true;
113 while ($left_line) {
114 $found = false;
115 $x = 1;
116 for ($y = 0; $y < $box['height']; $y++) {
117 $color = imagecolorat($img, $x, $y);
118 if ($color) {
119 $found = true;
120 break;
121 }
122 }
123 if (!$found) {
124 $left_line = false;
125 break;
126 } else {
127 $box['x'] ++;
128 $box['width'] ++;
129 imagedestroy($img);
130 $img = $this->ttf_tmp($value, $box, $size, $font);
131 }
132 }
133
134 //right add
135 $right_line = true;
136 while ($right_line) {
137 $found = false;
138 $x = $box['width'] - 1;
139 for ($y = 0; $y < $box['height']; $y++) {
140 $color = imagecolorat($img, $x, $y);
141 if ($color) {
142 $found = true;
143 break;
144 }
145 }
146 if (!$found) {
147 $right_line = false;
148 break;
149 } else {
150 $box['width'] ++;
151 imagedestroy($img);
152 $img = $this->ttf_tmp($value, $box, $size, $font);
153 }
154 }
155
156 //top trim
157 $found = false;
158 for ($y = 0; $y < $box['height']; $y++) {
159 for ($x = 0; $x < $box['width']; $x++) {
160 $color = imagecolorat($img, $x, $y);
161 if ($color) {
162 $found = true;
163 break;
164 }
165 }
166 if (!$found) {
167 $box['y'] --;
168 $box['height'] --;
169 } else {
170 break;
171 }
172 }
173
174
175 //bottom trim
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
193
194 //left trim
195 $found = false;
196 for ($x = 0; $x < $box['width']; $x++) {
197 for ($y = 0; $y < $box['height']; $y++) {
198 $color = imagecolorat($img, $x, $y);
199 if ($color) {
200 $found = true;
201 break;
202 }
203 }
204 if (!$found) {
205 $box['x'] --;
206 $box['width'] --;
207 } else {
208 break;
209 }
210 }
211
212
213 //right trim
214 $found = false;
215 for ($x = $box['width'] - 1; $x >= 0; $x--) {
216 for ($y = 0; $y < $box['height']; $y++) {
217 $color = imagecolorat($img, $x, $y);
218 if ($color) {
219 $found = true;
220 break;
221 }
222 }
223 if (!$found) {
224 $box['width'] --;
225 } else {
226 break;
227 }
228 }
229 return $box;
230 }
231
232 public function ttf_tmp($value, $box, $size, $font) {
233 $img = imagecreatetruecolor($box['width'], $box['height']);
234 $black = imagecolorallocate($img, 0, 0, 0);
235 $red = imagecolorallocate($img, 255, 0, 0);
236 imagefill($img, 0, 0, $black);
237 imagettftext($img, $size, 0, (int) $box['x'], $box['y'], $red, $font, $value);
238 imagecolordeallocate($img, $black);
239 imagecolordeallocate($img, $red);
240 return $img;
241 }
242
243 }
244