Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created September 17, 2012 19:26
Show Gist options
  • Save jfsiii/3739251 to your computer and use it in GitHub Desktop.
Save jfsiii/3739251 to your computer and use it in GitHub Desktop.
Give a url, get back a base64-encoded data URI
/**
* Given an image URL, return a base64-encoded data URI.
* @param {String} url|uri|image The URL for the image to be converted.
* @param {String} [callback|cb] Name of the function you want to be called.
* @returns {Object} JSON
* @returns {String} JSON.data The data URI for the image
* @returns {Object} JSON.meta
* @returns {Number} JSON.meta.width The width of the image, in pixels.
* @returns {Number} JSON.meta.height The height of the image, in pixels.
* @returns {Object} JSON.http
* @returns {Number} JSON.http.code The HTTP status code for the image request
* @returns {String} JSON.http.text The text associated with {JSON.http.code}
*/
jQuery.getJSON('//image2datauri.jit.su?cb=?', {
url: 'http://nodejs.org/images/logo-light.png'
}, function(data) {
var image = new Image();
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
image.src = data.data;
context.drawImage(image, 0, 0);
document.body.appendChild(canvas);
})
{
"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPUAAABCCAYAAACPfpCoAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAE3tJREFUeNrsXQt4VNW1XpOZPAYSwFQqtSJQL1ap/aqNV6ylVRQfqFBqvaKIQpHqraDVWq+F0uuj4KtCUWurVrTVgoAgUikWroAoBBBEEBHkEUReCRgeeSeTmen+M//cHIaZyTmTcyZncC++9UGGMyf7nL3XWv967LU94XBYMoU+PrBQ3tjyO/H7Orp+rOFwSLK9eTLkWxOlc7sebhhSB8V/UtxN8WTF8xXXypeQ3v38JXl310tqHXVw6Qg9cnvR1JTHlyWanJkWT5ZUNxyUJTufd8uQTld8ueI+imcpfk1xXz1Txx9poXaQcrzt5POKDbLl4HI3DCeouMHw81WK31D8iuKeera0UGsyaa1rA0ek5ND70hCsc+MQge+GKl6reJzirnrWtFBraoHaZ3eS1ftmSUVDmZuHma/4d4oXKv45h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/KTcp9s+zdvEIUhvlGqmok5WMg5ecOOfp4nkQ0WJpPcDFW1LxBhTpKEkfELQl1AwNMpQwSwEK8Junr4TyHC3c0FxneeNJ9pb1O7Ct+XwckK8zcHwvppxIpzFhlw3g3cEKHyNGHiTtFeCfjiTymij3BQGQvphAiPyHp6V/2OudglA0KCmv2H7RqvxUL2ZiGYI2ZKHgiBbKMBmIK0SygOaLjT9EwjZD4xy0V8bpXOOYobaayu15sDDBmEW5B46Cs8PfSNh1AK+hfFFFgkuYkc7350q/HqKa2qBasHlIifahVU4kyw8e8j5MyV6ztkLKDdtCfg/+1tBX3WUC3aiT9t3TSYULM6LlRqRSVYPFjpxby1x9I2+0820PjE/WtT6ewv0nDCEK++XG6NwMNvn8JlVERlZ2tOyWjlvptm/3m1ryoBWLidItwOKUYUiN93/582WZeJsYyiWjicWn7tNkiWrxbGFiz4u4MJsxb3sbPUEqLDTj7d5Pf2cr4zhVUqm1NHzBeAOX4ieHzSwix0VrpLbo32Ya19BTXEtBXjRMD+7L2DvvMYHXnJHA1QtS6iNYjneGmAwqgiBEMRFvZByV5OmU/rcJlXGhBFz3H+wxCXZ7ElUFs5CGirBfEXa2Oo24MhHuCAeUi74zzsIz7o4EUEVD9hdNr6cveEHA9gzg3yNHFMWvohw+Uo3szu40AXx+QyOaBGTECC9/kJQrDeHFn1V3Uei3kOJGD3sjPDlBgUFF2vzibxbAj7jGOwh3bWgn1B7dRqa5Ox2D0ho4IzSbshz/vpYXOpNNFPmawZaA0521fYCAnUwhoCVFhRIK/Q6FelWHrCEZiCIX7dKKKJZLmU1L+LcAA0cInBq5/XMQAAAAASUVORK5CYII=",
"meta": {
"width": 245,
"height": 66
},
"http": {
"code": 200,
"text": "OK"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment