function Uploader(options) {
this.exceptionHandler = options.exceptions || console.log.bind(console);
this.fileInput = document.querySelector(options.input);
if (this.fileInput === null) {
this.exceptionHandler("Could not find file input element: " + options.input);
return null;
}
this.allowedTypes = options.types || ["gif", "jpg", "jpeg", "png"];
this.reader = new FileReader();
}
Uploader.prototype.listen = function (callback) {
if (!this.fileInput) {
return;
}
this.fileInput.addEventListener("change", function(e) {
// Do not submit the form
e.preventDefault();
// Make sure one file was selected
if (!this.fileInput.files || this.fileInput.files.length !== 1) {
this.exceptionHandler("Please select one file");
} else {
this.fileReaderSetup(this.fileInput.files[0], callback);
}
}.bind(this));
};
Uploader.prototype.fileReaderSetup = function(file, callback) {
// Make sure the file is an image
if (this.validFileType(file.type)) {
// Read the image as base64 data
this.reader.readAsDataURL(file);
this.reader.addEventListener("load", function(e) {
// Call the callback with the image's base64 data when it's available
callback(e.target.result);
});
} else {
this.exceptionHandler("Invalid file type, please use one of: " + this.allowedTypes);
}
};
Uploader.prototype.validFileType = function(filename) {
// Get the second part of the MIME type
var extension = filename.split("/").pop().toLowerCase();
// See if it is in the array of allowed types
return this.allowedTypes.indexOf(extension) !== -1;
};
function Cropper(options) {
this.exceptionHandler = options.exceptions || console.log.bind(console);
if (!options.size) {
this.exceptionHandler("Size field in options is required");
return null;
}
this.imageCanvas = document.querySelector(options.canvas);
if (this.imageCanvas === null) {
this.exceptionHandler("Coud not find canvas element: "...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.