A supplemental script for Signature Pad that generates an image of the signature’s JSON output server-side using PHP.
Signature to Image is a simple PHP script that will take the JSON output of Signature Pad and generate an image file server-side to be saved for later. Uses the GD Image Library for image generation and PHP’s built in json_decode()
if a string is passed.
The whole signature to image generation requires just a few lines of PHP:
<?php
require_once 'signature-to-image.php';
$json = $_POST['output'];
$img = sigJsonToImage($json);
imagepng($img, 'signature.png');
imagedestroy($img);
First, include the required PHP file: signature-to-image.php
.
<?php
require_once 'signature-to-image.php';
Get the signature from the form post. Signature Pad defaults to naming the post field as output
.
<?php
$json = $_POST['output'];
Then, call the function, passing a string representing the JSON, submitted by Signature Pad, or an already decoded JSON object (using json_decode()
).
<?php
$img = sigJsonToImage($json);
The $img
variable will be an image resource, which you can either display immediately or save to a file.
<?php
imagepng($img, 'signature.png');
<?php
header('Content-Type: image/png');
imagepng($img);
When you have finished with the image resource, make sure to clean up after yourself and free some memory.
<?php
imagedestroy($img);
Method | Arguments | Return | Description |
---|---|---|---|
sigJsonToImage( |
$json:json|string String or decoded JSON representing the signature output $options:array Array of properties to change the default options |
image resource |
Redraws the signature as a GD Library image resource |
Options can be passed as the second argument of the function when called. Only options that are different from the defaults need to be included in the options array.
<?php
$img = sigJsonToImage($json, array('imageSize'=>array(198, 55)));
It’s highly recommended that the options used server-side match the values used in the Javascript.
Name | Type | Default Value | Description |
---|---|---|---|
imageSize |
array width, height |
array(198, 55) |
Determines the final output size of the image |
bgColour |
array
hex red, hex green, hex blue string
transparent |
array(0xff, 0xff, 0xff) |
The colour fill for the background of the image Transparency: |
penWidth |
int |
2 |
Thickness, in pixels, of the drawing pen |
penColour |
array hex red, hex green, hex blue |
array(0x14, 0x53, 0x94) |
Colour of the drawing ink |
drawMultiplier |
int |
12 |
The signature is enlarged by this factor before being redrawn to maintain a smooth finish. Reduce this only if you are having memory issues. |
Check out the changelog on GitHub.
Signature to Image is licensed under the New BSD license, which is included in the downloadable package.
- PHP to SVG: sigToSvg by Charles Gebhard is a script for converting the signature JSON to SVG using PHP. Check out the amazing sigToSvg on GitHub.
- Python: python-signpad2image by Alan Viars is a script for converting the signature JSON to PNG using Python. Check out the fantastico python-signpad2image on GitHub.
- Ruby on Rails: ruby-signaturepad-to-image.rb by Phil Hofmann is a chunk of code for converting signature JSON to PNG within a Ruby on Rails app. Check out the wam-bham ruby-signaturepad-to-image.rb on Github.
- C#: SignatureToDotNet by Curtis Herbert is a script for converting the signature JSON to an image using C#. Check out the awesome SignatureToDotNet project on GitHub.
- Perl: signature-to-image.pl by Jim Turner is a script for converting the signature JSON to a PNG using Perl. Check out the fabulous signature-to-image.pl on CPAN.
- ColdFusion: sigJsonToImage by James Moberg is a script for converting the signature JSON to an PNG using ColdFusion. Check out the super-duper sigJsonToImage project on CFLib.org.
- Java: SignatureToImageJava by Vinod Kiran is a script for converting the signature JSON to an image using Java. Check out the rad SignatureToImageJava project on GitHub.