Skip to content

Instantly share code, notes, and snippets.

@madhums
Created September 14, 2014 17:37
Show Gist options
  • Save madhums/e749dca107e26d72b64d to your computer and use it in GitHub Desktop.
Save madhums/e749dca107e26d72b64d to your computer and use it in GitHub Desktop.

Revisions

  1. madhums revised this gist Sep 14, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions base64-image-upload.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    /*
    * Taken from http://stackoverflow.com/questions/5867534/how-to-save-canvas-data-to-file/5971674#5971674
    */

    var fs = require('fs');
    // string generated by canvas.toDataURL()
    var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
  2. madhums created this gist Sep 14, 2014.
    9 changes: 9 additions & 0 deletions base64-image-upload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    var fs = require('fs');
    // string generated by canvas.toDataURL()
    var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
    + "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
    + "3gAAAABJRU5ErkJggg==";
    // strip off the data: url prefix to get just the base64-encoded bytes
    var data = img.replace(/^data:image\/\w+;base64,/, "");
    var buf = new Buffer(data, 'base64');
    fs.writeFile('image.png', buf);