Last active
April 5, 2018 15:34
-
-
Save nybblr/d6073b742976104113a0597fbf159e0b to your computer and use it in GitHub Desktop.
In-browser image resizing + processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processImage from 'process-image'; | |
var file = /* File() object from drag-n-drop or file input */ | |
var imageProcessOpts = { | |
maxWidth: 1200, | |
maxHeight: 1200, | |
quality: 0.6 | |
}; | |
processImage(file, imageProcessOpts, blob => { | |
this.set('resizeBlob', blob); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getFormData = (data) => { | |
var formData = new FormData(); | |
Object.keys(data).forEach(key => { | |
var value = data[key]; | |
if (value !== undefined) { | |
var filename = value instanceof Blob ? value.name : undefined; | |
formData.append(key, value, filename); | |
} | |
}); | |
return formData; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"fileapi": "^2.0.20" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FileAPI = window.FileAPI; | |
var quality = 0.6; | |
var type = 'image/jpeg'; | |
export default function processImage(file, opts={}, fn) { | |
opts = { | |
type, | |
quality, | |
...opts | |
}; | |
FileAPI.Image.transform(file, { hd: opts }, true, | |
(error, img) => { | |
img.hd.toBlob(blob => { | |
// Transfer name from file | |
blob.name = file.name; | |
fn(blob); | |
}, opts.type, opts.quality); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment