Php jSignature jQuery插件-从存储在DB(base30)中的签名生成图像
使用此命令保存图像后Php jSignature jQuery插件-从存储在DB(base30)中的签名生成图像,php,jquery,laravel,jsignature,Php,Jquery,Laravel,Jsignature,使用此命令保存图像后 $("#canvas").jSignature("getData", "base30"); 我们可以将该数据存储到DB中,它是一个简单的字符串: data:image/jsignature;base30,9MZ4555665655465644644645433232100Y2333465656786456646464443Z54334433222Y13344544486666667775353646443_2xZ
$("#canvas").jSignature("getData", "base30");
我们可以将该数据存储到DB中,它是一个简单的字符串:
data:image/jsignature;base30,9MZ4555665655465644644645433232100Y2333465656786456646464443Z54334433222Y13344544486666667775353646443_2xZ110000000Y1111111222223222344555332220100000Z11111121212130Y122222333343222111100000Z1111212222122212
现在如何从“base30”签名制作图像?我们可以将其转换为任何内容,以便在网站上显示吗
或者有一种方法可以禁用对画布的编辑。防止更改签名
更新
我做了一个小的变通方法,效果很好
我创建了一个不可见的div,在这里我从DB加载jSignature并使其不可见,然后从中取出svg并在页面中显示该svg
<div id="oldSignature" style="display: none"></div>
<object id="img" type="image/svg+xml" data="">
Your browser doesn't support SVG!
</object>
在上的“附加”文件夹中查找。这里有php和DotNet的例子 关于Base30格式 base30(别名图像/jSignature;base30)(导出和导入)(矢量) 数据格式是一种Base64精神的压缩格式,专为荒谬而设计 紧凑性和本地url兼容性。它是“本地”数据 结构压缩为所有 向量代码示例(.Net,Python)详细说明了此文件的解压缩 将格式转换为可渲染形式(SVG、语言本机坐标数组) 在附加文件夹中提供。一种可能的解决方法 将数据传输到服务器的是JSONP,它具有实用的功能 URL长度限制(当然由IE规定)不超过2000+ 人物。此压缩格式与URL本机兼容,无需 需要重新编码,但大多数情况下可容纳2000个字符 非复杂签名
编辑-对于那些没有成功使用/extras示例的人,这里可能有一些有用的帮助/示例代码。这里有太多的细节 我使用了上面的答案,在函数中做了一点修改,得到了以下内容。这对我有用
<?php
ini_set ( 'display_errors', E_ALL );
require_once ('jSignature_Tools_Base30.php');
function base30_to_jpeg($base30_string, $output_file) {
$data = str_replace ( 'image/jsignature;base30,', '', $base30_string );
$converter = new jSignature_Tools_Base30 ();
$raw = $converter->Base64ToNative ( $data );
// Calculate dimensions
$width = 0;
$height = 0;
foreach ( $raw as $line ) {
if (max ( $line ['x'] ) > $width)
$width = max ( $line ['x'] );
if (max ( $line ['y'] ) > $height)
$height = max ( $line ['y'] );
}
// Create an image
$im = imagecreatetruecolor ( $width + 20, $height + 20 );
// Save transparency for PNG
imagesavealpha ( $im, true );
// Fill background with transparency
$trans_colour = imagecolorallocatealpha ( $im, 255, 255, 255, 127 );
imagefill ( $im, 0, 0, $trans_colour );
// Set pen thickness
imagesetthickness ( $im, 2 );
// Set pen color to black
$black = imagecolorallocate ( $im, 0, 0, 0 );
// Loop through array pairs from each signature word
for($i = 0; $i < count ( $raw ); $i ++) {
// Loop through each pair in a word
for($j = 0; $j < count ( $raw [$i] ['x'] ); $j ++) {
// Make sure we are not on the last coordinate in the array
if (! isset ( $raw [$i] ['x'] [$j] ))
break;
if (! isset ( $raw [$i] ['x'] [$j + 1] ))
// Draw the dot for the coordinate
imagesetpixel ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $black );
else
// Draw the line for the coordinate pair
imageline ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $raw [$i] ['x'] [$j + 1], $raw [$i] ['y'] [$j + 1], $black );
}
}
// Check if the image exists
if (! file_exists ( dirname ( $output_file ) )) {
mkdir(dirname($output_file));
}
// Create Image
$ifp = fopen ( $output_file, "wb" );
imagepng ( $im, $output_file );
fclose ( $ifp );
imagedestroy ( $im );
return $output_file;
}
// test
$imgStr ='image/jsignature;base30,7U010100010000000001_1G88b8ace99aab756856_bE2000000010000000101_1D6689cbaa9b956558564_8w757698987766566556545_3PZ2110101010100000000Y10_fF0000Z2100001000Y110_1V9789cb86966655475_fK_1x';
base30_to_jpeg ( $imgStr, 'test.png' );
?>
好的,我找到了这个,但我不知道如何应用它。把php_base30代码放在哪里?没有例子。您亲自尝试过吗?您使用什么开发环境/语言?我猜您寻求的解决方案是从服务器的db中提取base30,然后生成一个图像,浏览器将其作为标准图像格式(如jpg或png)接收-对吗?是的,没错。我使用laravel框架,但我不知道如何做到这一点
<?php
ini_set ( 'display_errors', E_ALL );
require_once ('jSignature_Tools_Base30.php');
function base30_to_jpeg($base30_string, $output_file) {
$data = str_replace ( 'image/jsignature;base30,', '', $base30_string );
$converter = new jSignature_Tools_Base30 ();
$raw = $converter->Base64ToNative ( $data );
// Calculate dimensions
$width = 0;
$height = 0;
foreach ( $raw as $line ) {
if (max ( $line ['x'] ) > $width)
$width = max ( $line ['x'] );
if (max ( $line ['y'] ) > $height)
$height = max ( $line ['y'] );
}
// Create an image
$im = imagecreatetruecolor ( $width + 20, $height + 20 );
// Save transparency for PNG
imagesavealpha ( $im, true );
// Fill background with transparency
$trans_colour = imagecolorallocatealpha ( $im, 255, 255, 255, 127 );
imagefill ( $im, 0, 0, $trans_colour );
// Set pen thickness
imagesetthickness ( $im, 2 );
// Set pen color to black
$black = imagecolorallocate ( $im, 0, 0, 0 );
// Loop through array pairs from each signature word
for($i = 0; $i < count ( $raw ); $i ++) {
// Loop through each pair in a word
for($j = 0; $j < count ( $raw [$i] ['x'] ); $j ++) {
// Make sure we are not on the last coordinate in the array
if (! isset ( $raw [$i] ['x'] [$j] ))
break;
if (! isset ( $raw [$i] ['x'] [$j + 1] ))
// Draw the dot for the coordinate
imagesetpixel ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $black );
else
// Draw the line for the coordinate pair
imageline ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $raw [$i] ['x'] [$j + 1], $raw [$i] ['y'] [$j + 1], $black );
}
}
// Check if the image exists
if (! file_exists ( dirname ( $output_file ) )) {
mkdir(dirname($output_file));
}
// Create Image
$ifp = fopen ( $output_file, "wb" );
imagepng ( $im, $output_file );
fclose ( $ifp );
imagedestroy ( $im );
return $output_file;
}
// test
$imgStr ='image/jsignature;base30,7U010100010000000001_1G88b8ace99aab756856_bE2000000010000000101_1D6689cbaa9b956558564_8w757698987766566556545_3PZ2110101010100000000Y10_fF0000Z2100001000Y110_1V9789cb86966655475_fK_1x';
base30_to_jpeg ( $imgStr, 'test.png' );
?>