jSignature的第一个字母捕获渲染不正确

时间:2014-03-11 18:02:04

标签: php

我刚刚开始使用jSignature并让它正常工作以将签名保存为png文件。但是,如果签名是一个首字母然后是一个空格,那么它的名字就会变成一条很长的粗线,但其余部分却是是准确的。为了解决它目前我用破折号签名然后是首字母后跟姓氏并替换行

// Loop through array pairs from each signature word
for ($i = 0; $i < count($a); $i++)

// Loop through array pairs from each signature word
for ($i = 1; $i < count($a); $i++)

从而忽略破折号的输出。

这显然不适合其他用户。任何帮助解决这个问题都将非常感激。

// signature div initialised

$(document).ready(function(){
  var $sigdiv = $("#signature")

  $sigdiv.jSignature()

  $('#clearSignature').click(function (){
    $sigdiv.jSignature("reset");
  })

})


// Capture signature and post
$.ajax({
    type: 'POST',
    url: "ajaxSaveSig.php",
    data: { signature: $('#signature').jSignature("getData", "base30"), clientid: clientid }
});


//convert signature from base30 to png and save - $data imploded to conert to string from array as jSignature_Tools_Base30.php gave an error without this line.

<?php
session_start();
// Get signature string from _POST
$data = $_POST['signature'];
$clientid = $_POST['clientid'];
$data = str_replace('image/jsignature;base30,', '', $data);
$data = implode($data);

include 'jSignature_Tools_Base30.php';

// Create jSignature object
$signature = new jSignature_Tools_Base30();

// Decode base30 format
$a = $signature->Base64ToNative($data);

// Create a image            
$im = imagecreatetruecolor(1295, 328);

// Save transparency for PNG
imagesavealpha($im, true);

// Fill background with transparency
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

// Set pen thickness
imagesetthickness($im, 4);

// Set pen color to blue            
$blue = imagecolorallocate($im, 0, 0, 255);

// Loop through array pairs from each signature word
for ($i = 0; $i < count($a); $i++)
{
    // Loop through each pair in a word
    for ($j = 0; $j < count($a[$i]['x']); $j++)
    {
         // Make sure we are not on the last coordinate in the array
         if ( ! isset($a[$i]['x'][$j]) or ! isset($a[$i]['x'][$j+1])) break;
              // Draw the line for the coordinate pair
              imageline($im, $a[$i]['x'][$j], $a[$i]['y'][$j], $a[$i]['x'][$j+1], $a[$i]['y'][$j+1], $blue);
         }
    }

    // Save image to a folder   
    $tm = time(); 
    $filename = '../inv/signatures/'.$clientid.'-'.$tm.'.png'; // Make folder path is writeable
    imagepng($im, $filename); // Removing $filename will output to browser instead of saving

    $_SESSION['s_signature'] = $filename;

    // Clean up
    imagedestroy($im);

?>

1 个答案:

答案 0 :(得分:0)

在搜索到同样问题的答案后数小时,我发现了你的帖子。我原本以为Base64ToNative中有一个错误(我甚至提交了一个错误报告),但看起来我似乎错误地使用了保存的图像数据。丑陋的锯齿线是不剥离所存储的图像数据的标题部分的结果。导出的图像数据字符串将如下所示,例如:

    "image/jSignature;base30,HEhda1ZDGAD_EDFddjeAD"
  1. 获取从数据库中提取的base30图像数据,并拆分前导&#34; image / jsignature;&#34;标题(如果我想处理多种格式)。
  2. 拆分&#34; base30,&#34;编码部分数据(如果我想稍后处理不同的编码)。

    // Data pulled from database is stored in $imgStr
    // splits the first part
    $split = "";
    list($type, $split) = explode(";", $imgStr);
    
    // removes the raw encoded image data
    list($encType, $split) = explode(",", $split);
    
  3. 然后,只需使用保存的图像字符串的数据部分进行渲染

    $converter = new jSignature_Tools_Base30();
    
    // $split now just contains "HEhda1ZDGAD_EDFddjeAD"
    $raw = $converter->Base64ToNative($split);
    
  4. 您现在可以继续使用相同的算法

        // Create a image
        $im = imagecreatetruecolor(800, 200);
    
        // 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, 5);
    
        // Set pen color to blue
        $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]) or ! isset($raw[$i]['x'][$j+1])) break;
                // 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);
            }
        }
    
        // start an output buffer to write the raw image data to
        ob_start();
           imagepng($im);
           $out = ob_get_contents();
        ob_end_clean();
    
        // Do something with the raw image data...
        <custom image handling code>
    
        // clean up the image resource handle
        imagedestroy($im);
    

    为了公平对待作者,我发现了从http://www.reloadedpc.com/php/jquery-jsignature-php-base30-image/处理jSignature原生数组的逻辑