brinley / jSignature

jQuery plugin for adding web signature functionality
http://www.unbolt.net/jSignature
693 stars 530 forks source link

Convert Base30 to PNG #97

Closed galebnas closed 8 years ago

galebnas commented 8 years ago

Hi First thank you for this great plugin. everything is working perfectly I am storing the Signatures in the database using the base30 type witch is very good and small in size but unfortunately I cant find a way to convert it to PNG. do you have any php code to convert the base30 to png.

Note: I tried the SignatureDataConversion_PHP class in the extra folder but I didn't manage to understand it or utilize it, a small working sample code would be great Thanks again :)

galebnas commented 8 years ago

Hi This happens every time I post an issue on github (I find the solution) This is a PHP function that will convert base30 to png it is based on your 'jSignature_Tools_Base30.php'

<?php
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);
        }
    } 

//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');

?>
itajackass commented 6 years ago

Is there a way to prevent to save file to disk and display inline image with base64 encoding? (always from base30)

kukode commented 4 years ago

hello @galebnas i already implement you code,but for show image with id from database how??