| @@ -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,49 +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 | - 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]); | |
| 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); | |
| 44 | 59 | } |
| 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 | 60 | } |
| 57 | 61 | |
| 58 | - return $value; | |
| 62 | + return $response; | |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | 65 | /* |
| 62 | 66 | * https://github.com/unlocomqx/text-measure |
| @@ -65,9 +69,8 @@ | ||
| 65 | 69 | public function ttf_box_fix($value, $box, $size, $font) { |
| 66 | 70 | |
| 67 | 71 | $img = $this->ttf_tmp($value, $box, $size, $font); |
| 68 | 72 | |
| 69 | - //top add | |
| 70 | 73 | $top_line = true; |
| 71 | 74 | while ($top_line) { |
| 72 | 75 | $found = false; |
| 73 | 76 | $y = 1; |
| @@ -81,17 +84,15 @@ | ||
| 81 | 84 | if (!$found) { |
| 82 | 85 | $top_line = false; |
| 83 | 86 | break; |
| 84 | 87 | } else { |
| 85 | - $box['y'] ++; | |
| 86 | - $box['height'] ++; | |
| 88 | + $box['y']++; | |
| 89 | + $box['height']++; | |
| 87 | 90 | imagedestroy($img); |
| 88 | 91 | $img = $this->ttf_tmp($value, $box, $size, $font); |
| 89 | 92 | } |
| 90 | 93 | } |
| 91 | 94 | |
| 92 | - | |
| 93 | - //bottom add | |
| 94 | 95 | $bottom_line = true; |
| 95 | 96 | while ($bottom_line) { |
| 96 | 97 | $found = false; |
| 97 | 98 | $y = $box['height'] - 1; |
| @@ -105,15 +106,14 @@ | ||
| 105 | 106 | if (!$found) { |
| 106 | 107 | $bottom_line = false; |
| 107 | 108 | break; |
| 108 | 109 | } else { |
| 109 | - $box['height'] ++; | |
| 110 | + $box['height']++; | |
| 110 | 111 | imagedestroy($img); |
| 111 | 112 | $img = $this->ttf_tmp($value, $box, $size, $font); |
| 112 | 113 | } |
| 113 | 114 | } |
| 114 | 115 | |
| 115 | - //left add | |
| 116 | 116 | $left_line = true; |
| 117 | 117 | while ($left_line) { |
| 118 | 118 | $found = false; |
| 119 | 119 | $x = 1; |
| @@ -127,16 +127,15 @@ | ||
| 127 | 127 | if (!$found) { |
| 128 | 128 | $left_line = false; |
| 129 | 129 | break; |
| 130 | 130 | } else { |
| 131 | - $box['x'] ++; | |
| 132 | - $box['width'] ++; | |
| 131 | + $box['x']++; | |
| 132 | + $box['width']++; | |
| 133 | 133 | imagedestroy($img); |
| 134 | 134 | $img = $this->ttf_tmp($value, $box, $size, $font); |
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - //right add | |
| 139 | 138 | $right_line = true; |
| 140 | 139 | while ($right_line) { |
| 141 | 140 | $found = false; |
| 142 | 141 | $x = $box['width'] - 1; |
| @@ -150,15 +149,14 @@ | ||
| 150 | 149 | if (!$found) { |
| 151 | 150 | $right_line = false; |
| 152 | 151 | break; |
| 153 | 152 | } else { |
| 154 | - $box['width'] ++; | |
| 153 | + $box['width']++; | |
| 155 | 154 | imagedestroy($img); |
| 156 | 155 | $img = $this->ttf_tmp($value, $box, $size, $font); |
| 157 | 156 | } |
| 158 | 157 | } |
| 159 | 158 | |
| 160 | - //top trim | |
| 161 | 159 | $found = false; |
| 162 | 160 | for ($y = 0; $y < $box['height']; $y++) { |
| 163 | 161 | for ($x = 0; $x < $box['width']; $x++) { |
| 164 | 162 | $color = imagecolorat($img, $x, $y); |
| @@ -167,17 +165,15 @@ | ||
| 167 | 165 | break; |
| 168 | 166 | } |
| 169 | 167 | } |
| 170 | 168 | if (!$found) { |
| 171 | - $box['y'] --; | |
| 172 | - $box['height'] --; | |
| 169 | + $box['y']--; | |
| 170 | + $box['height']--; | |
| 173 | 171 | } else { |
| 174 | 172 | break; |
| 175 | 173 | } |
| 176 | 174 | } |
| 177 | 175 | |
| 178 | - | |
| 179 | - //bottom trim | |
| 180 | 176 | $found = false; |
| 181 | 177 | for ($y = $box['height'] - 1; $y >= 0; $y--) { |
| 182 | 178 | for ($x = 0; $x < $box['width']; $x++) { |
| 183 | 179 | $color = imagecolorat($img, $x, $y); |
| @@ -186,17 +182,14 @@ | ||
| 186 | 182 | break; |
| 187 | 183 | } |
| 188 | 184 | } |
| 189 | 185 | if (!$found) { |
| 190 | - $box['height'] --; | |
| 186 | + $box['height']--; | |
| 191 | 187 | } else { |
| 192 | 188 | break; |
| 193 | 189 | } |
| 194 | 190 | } |
| 195 | 191 | |
| 196 | - | |
| 197 | - | |
| 198 | - //left trim | |
| 199 | 192 | $found = false; |
| 200 | 193 | for ($x = 0; $x < $box['width']; $x++) { |
| 201 | 194 | for ($y = 0; $y < $box['height']; $y++) { |
| 202 | 195 | $color = imagecolorat($img, $x, $y); |
| @@ -205,17 +198,15 @@ | ||
| 205 | 198 | break; |
| 206 | 199 | } |
| 207 | 200 | } |
| 208 | 201 | if (!$found) { |
| 209 | - $box['x'] --; | |
| 210 | - $box['width'] --; | |
| 202 | + $box['x']--; | |
| 203 | + $box['width']--; | |
| 211 | 204 | } else { |
| 212 | 205 | break; |
| 213 | 206 | } |
| 214 | 207 | } |
| 215 | 208 | |
| 216 | - | |
| 217 | - //right trim | |
| 218 | 209 | $found = false; |
| 219 | 210 | for ($x = $box['width'] - 1; $x >= 0; $x--) { |
| 220 | 211 | for ($y = 0; $y < $box['height']; $y++) { |
| 221 | 212 | $color = imagecolorat($img, $x, $y); |
| @@ -224,9 +215,9 @@ | ||
| 224 | 215 | break; |
| 225 | 216 | } |
| 226 | 217 | } |
| 227 | 218 | if (!$found) { |
| 228 | - $box['width'] --; | |
| 219 | + $box['width']--; | |
| 229 | 220 | } else { |
| 230 | 221 | break; |
| 231 | 222 | } |
| 232 | 223 | } |
| @@ -243,5 +234,125 @@ | ||
| 243 | 234 | imagecolordeallocate($img, $red); |
| 244 | 235 | return $img; |
| 245 | 236 | } |
| 246 | 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 | + } | |
| 247 | 358 | } |