Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function resizeImage(f) {
- var file = f,
- fileType = file.type,
- reader = new FileReader();
- reader.onloadend = function() {
- var image = new Image();
- image.src = reader.result;
- image.onload = function() {
- var maxWidth = 250,
- maxHeight = 250,
- imageWidth = image.width,
- imageHeight = image.height;
- if (imageWidth > imageHeight) {
- if (imageWidth > maxWidth) {
- imageHeight *= maxWidth / imageWidth;
- imageWidth = maxWidth;
- }
- }
- else {
- if (imageHeight > maxHeight) {
- imageWidth *= maxHeight / imageHeight;
- imageHeight = maxHeight;
- }
- }
- var canvas = document.createElement('canvas');
- canvas.width = imageWidth;
- canvas.height = imageHeight;
- var ctx = canvas.getContext("2d");
- ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
- // The resized file ready for upload
- finalFile = dataURItoBlob(canvas.toDataURL(fileType));
- }
- }
- reader.readAsDataURL(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment