Skip to content

Instantly share code, notes, and snippets.

@branch14
Created December 11, 2012 14:15
Show Gist options
  • Select an option

  • Save branch14/4258871 to your computer and use it in GitHub Desktop.

Select an option

Save branch14/4258871 to your computer and use it in GitHub Desktop.
convert json signatures captured by thomasjbradley's signature-pad to an image
# see https://github.com/thomasjbradley/signature-pad for more details
instructions = JSON.load(data).map { |h| "line #{h['mx']},#{h['my']} #{h['lx']},#{h['ly']}" } * ' '
system "convert -size 198x55 xc:transparent -stroke blue -draw '#{instructions}' signature.png"
@kratob
Copy link
Copy Markdown

kratob commented Feb 20, 2013

Thanks, this is pretty awesome!

If data is user input it is however also quite unsafe. I'd suggest

require 'open3'
instructions = JSON.parse(data).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '
Open3.popen3("convert -size 198x155 xc:transparent -stroke blue -draw @- signature.png") do |input, output, error|
  input.puts instructions
end

@nitj2021
Copy link
Copy Markdown

nitj2021 commented Mar 1, 2013

i want to save the image to paperclip . can you give some help

@styliii
Copy link
Copy Markdown

styliii commented Mar 10, 2013

Thank you so much. This is great, using it my project.

@fwidtmann
Copy link
Copy Markdown

i get some weird errors:

convert.exe: Non-conforming drawing primitive definition line' @ error/draw.c/DrawImage/3154. convert.exe: unable to open image116,28': No such file or directory @ error/blob.c/OpenBlob/2641.
convert.exe: no decode delegate for this image format `116,28' @ error/constitute.c/ReadImage/550.

@kzink
Copy link
Copy Markdown

kzink commented Apr 16, 2013

For those wanting to use paperclip or other file attachment plugins, this seems to work pretty well...

instructions = JSON.parse(data).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '
tempfile = Tempfile.new(["signature", '.png'])
Open3.popen3("convert -size 198x55 xc:transparent -stroke blue -draw @- #{tempfile.path}") do |input, output, error|
    input.puts instructions
end
@your_model.your_attachment = tempfile

@hadees
Copy link
Copy Markdown

hadees commented Oct 28, 2013

Thanks for the code although hard coding the image size in convert seems like a bad idea to me.

@drazen
Copy link
Copy Markdown

drazen commented Apr 11, 2014

In case you're using Mongoid and would like to store the signature directly in mongodb...

field :image, type: Moped::BSON::Binary

...

instructions = JSON.parse(self.strokes).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '
Open3.popen3("convert -size #{WIDTH}x#{HEIGHT} xc:transparent -stroke blue -draw @- PNG:-") do |input, output, error|
      input.puts instructions
      input.close
      self.image = Moped::BSON::Binary.new(:generic, Base64.encode64(output.read))
end

to display ...

= image_tag "data:image/png;base64,#{@your_object.image}"

@Frank004
Copy link
Copy Markdown

Frank004 commented May 2, 2014

Can you create a demo app for us noobs on RoR

@soffes
Copy link
Copy Markdown

soffes commented Jul 2, 2014

@Frank004 check out shomei

@Frank004
Copy link
Copy Markdown

im getting this error with paperclip Paperclip::AdapterRegistry::NoHandlerError
the before_save its not running
some body got sample with paperclip.

@soffes thanks but cant get it to work with paperclip with that one
cant get to convert the data

@lennypistorio
Copy link
Copy Markdown

I'm having issues with this as well. I get the error Paperclip::AdapterRegistry::NoHandlerError and No handler found for "[{.

Any suggestions??

Thanks!

@jterrero
Copy link
Copy Markdown

Does anyone have any examples on how to actually, save signature to database using rails?

@PatrickatPaperlessPCS
Copy link
Copy Markdown

Deployed this successfully - unfortunately the signature doesn't fit in the form well. It cuts off portions of the signature - any ideas on how to resize not just the pad as shown in the documentation, but the signature itself?

@chrislebaron
Copy link
Copy Markdown

Has anyone done this with CarrierWave? I'm new to rails, and don't have a clue how to even get started with this.

@rizwan-munir-7
Copy link
Copy Markdown

@kzink
paperclip attachment gives error " content type discovered from file command: inode/x-empty " and signature image is not attached to model

@dgm
Copy link
Copy Markdown

dgm commented Jan 3, 2019

-draw @- seems to return without asking for STDIN input on OS X now.

@lrbz
Copy link
Copy Markdown

lrbz commented Mar 19, 2020

is there any way to adjust the stroke font size ?

Copy link
Copy Markdown

ghost commented Jul 13, 2020

@lrbz - checkout signature-pad's options'. By default stroke is 2px wide, but you can adjust it :)

@zairlaka
Copy link
Copy Markdown

zairlaka commented Dec 9, 2022

create file field hidden id profile_signature for attachment

var canvas = document.querySelector("canvas");
  var imfile;
  canvas.toBlob((blob) => {
    imfile = new File([blob], "signature.jpg", { type: "image/jpeg" })
    let dt = new DataTransfer();
    dt.items.add(imfile);
    document.getElementById("profile_signature").files = dt.files;
  });

first it will convert base64 into image and then attach it to your hidden field

@harshitDispatch
Copy link
Copy Markdown

create file field hidden id profile_signature for attachment

var canvas = document.querySelector("canvas");
  var imfile;
  canvas.toBlob((blob) => {
    imfile = new File([blob], "signature.jpg", { type: "image/jpeg" })
    let dt = new DataTransfer();
    dt.items.add(imfile);
    document.getElementById("profile_signature").files = dt.files;
  });

first it will convert base64 into image and then attach it to your hidden field

Here, the field "profile_signature" will look like what in HTML?

@branch14
Copy link
Copy Markdown
Author

branch14 commented Oct 3, 2023

@harshitDispatch Looks like it should be a <input type="file">

@harshitDispatch
Copy link
Copy Markdown

harshitDispatch commented Oct 3, 2023

@harshitDispatch Looks like it should be a <input type="file">

@branch14 Yeah that worked. Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment