admin管理员组文章数量:1403348
I'm writing a cross-platform mobile application using phonegap, and i have a file-upload input for image uploading of single images.
The problem is that most pictures being uploaded are ones taken using the mobile phone which are around 4MB in size.
I want to shrink those images dramatically, as i don't need them in high quality at all.
Also, i need them converted to base64 and not in real image file. (That i already have using FileReader)
Any ideas how to achieve this? Maybe using canvas or something?
Update: here is what i have so far:
function shrink() {
var self = this;
var reader = new FileReader(); // init a file reader
var file = $('#file-input').prop('files')[0]; // get file from input
reader.onloadend = function() {
// shrink image
var image = document.createElement('img');
image.src = reader.result;
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, 300, 300);
var shrinked = canvas.toDataURL('image/jpeg');
console.log(shrinked);
};
reader.readAsDataURL(file); // convert file to base64*/
}
but all i get is a black image Thanks
I'm writing a cross-platform mobile application using phonegap, and i have a file-upload input for image uploading of single images.
The problem is that most pictures being uploaded are ones taken using the mobile phone which are around 4MB in size.
I want to shrink those images dramatically, as i don't need them in high quality at all.
Also, i need them converted to base64 and not in real image file. (That i already have using FileReader)
Any ideas how to achieve this? Maybe using canvas or something?
Update: here is what i have so far:
function shrink() {
var self = this;
var reader = new FileReader(); // init a file reader
var file = $('#file-input').prop('files')[0]; // get file from input
reader.onloadend = function() {
// shrink image
var image = document.createElement('img');
image.src = reader.result;
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, 300, 300);
var shrinked = canvas.toDataURL('image/jpeg');
console.log(shrinked);
};
reader.readAsDataURL(file); // convert file to base64*/
}
but all i get is a black image Thanks
Share Improve this question edited Sep 9, 2015 at 14:28 Michael 3,3365 gold badges26 silver badges37 bronze badges asked Mar 10, 2013 at 21:59 shaharmorshaharmor 1,7364 gold badges15 silver badges26 bronze badges 1- 1 stackoverflow./questions/2434458/… (which leads to hacks.mozilla/2011/01/how-to-develop-a-html5-image-uploader) – Sygmoral Commented Mar 10, 2013 at 22:01
2 Answers
Reset to default 2Found the answer.
The problem was that i wasn't waiting for the image to fully load before drawing it.
once i added
image.onload = function() {
}
and ran everything inside it works.
I know this an old thread, but I had the same question about where to place the "on load" and this worked for me...
navigator.camera.getPicture(function (imageURI) {
console.log("*** capture success. uri length...", imageURI.length);
var image = document.createElement('img');
image.onload = function() {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, 300, 300);
var shrunk = canvas.toDataURL('image/jpeg');
console.log(shrunk);
// used shrunk here
}
image.src = imageURI; // triggers the onload
}
pat
本文标签: Shrink image before uploading with javascriptStack Overflow
版权声明:本文标题:Shrink image before uploading with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744386655a2603772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论