Skip to content

Instantly share code, notes, and snippets.

@Themaister
Created September 17, 2011 19:11
Show Gist options
  • Select an option

  • Save Themaister/1224240 to your computer and use it in GitHub Desktop.

Select an option

Save Themaister/1224240 to your computer and use it in GitHub Desktop.
function load_image(evt) {
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
alert("FileReader API not supported by this browser ...");
return;
}
var file = evt.target.files[0];
if (!file.type.match("image.*")) {
alert("This is not an image file! :(");
return;
}
var reader = new FileReader();
reader.onload = (function(file) {
return function(e) {
alert("File read!");
texture_.old_img = texture_.image;
texture_.image = new Image();
texture_.image.onload = function() {
if (texture_.image.width > 0 && texture_.image.height > 0) {
try {
set_image(texture_.image);
} catch (e) {
texture_.image = texture_.old_img;
alert(e);
}
} else {
texture_.image = texture_.old_img;
}
}
texture_.image.src = e.target.result;
}
})(file);
reader.readAsDataURL(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment