Skip to content

Instantly share code, notes, and snippets.

@azami
Last active August 29, 2015 14:04
Show Gist options
  • Save azami/65d0c00998f7f2d5fd2f to your computer and use it in GitHub Desktop.
Save azami/65d0c00998f7f2d5fd2f to your computer and use it in GitHub Desktop.
画像ファイルのサイズ取得とサムネイル作成
var createPreview = function(file, image, callback) {
var canvas = $('<canvas>')[0];
var context = canvas.getContext('2d');
var width = image.width;
var height = image.height;
if (image.width > previewOptions.maxWidth) {
width = previewOptions.maxWidth;
height = previewOptions.maxWidth / image.width * image.height;
}
if (image.height > previewOptions.maxHeight) {
height = previewOptions.maxHeight;
width = previewOptions.maxHeight / image.height * image.width;
}
canvas.width = width;
canvas.height = height;
context.drawImage(image, 0, 0, width, height);
file.preview = canvas.toDataURL(file.type);
callback();
};
var loadImage = function(file, callback) {
var reader, image;
reader = new FileReader();
image = new Image();
image.onload = function() {
file.width = image.width;
file.height = image.height;
createPreview(file, image, callback);
};
reader.onload = function() {
image.src = reader.result;
};
reader.readAsDataURL(file);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment