Skip to content

Instantly share code, notes, and snippets.

@hSATAC
Forked from timdream/jquery.imgurupload.js
Created October 27, 2011 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hSATAC/1318737 to your computer and use it in GitHub Desktop.
Save hSATAC/1318737 to your computer and use it in GitHub Desktop.
/*!
*
* Imgur CORS Image upload
*
* Author & Copyright: timdream (timdream@gmail.com; http://timdream.org/)
* License: MIT LICENSE
*
*/
"use strict";
(function ($) {
$.imgurUploadSupported = $.support.cors;
$.fn.imgurUpload = function (options) {
if (!$.imgurUploadSupported) return this;
var settings = {
key: '',
name: 'image.png',
title: false,
caption: false,
beforeSend: $.noop,
complete: $.noop,
success: $.noop,
error: $.noop
};
if (options) {
$.extend(settings, options);
}
return this.each(
function () {
if (this.nodeName.toLowerCase() !== 'canvas') return;
$.ajax(
{
url: 'http://api.imgur.com/2/upload.json',
type: 'POST',
data: (function (canvas) {
var d = {
type: 'base64',
key: settings.key,
image: canvas.toDataURL().split('base64,')[1]
};
if (settings.name !== false) d.name = settings.name;
if (settings.title !== false) d.title = settings.title;
if (settings.caption !== false) d.caption = settings.caption;
return d;
})(this),
dataType: 'json',
beforeSend: settings.beforeSend,
complete: settings.complete,
success: settings.success,
error: settings.error
}
);
}
);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment