我有这个脚本,它用于获取浏览器上传图像的宽度和高度。
参考:http://renevier.net/misc/resizeimg.html http://renevier.net/misc/resizeimg.html
function createReader(file) {
reader.onload = function(evt) {
var image = new Image();
image.onload = function(evt) {
var width = this.width;
var height = this.height;
alert (width); // will produce something like 198
};
image.src = evt.target.result;
};
reader.readAsDataURL(file);
}
for (var i = 0, length = input.files.length; i < length; i++) {
createReader(input.files[i]);
}
我想从外部访问值宽度和高度createReader
功能。我怎样才能做到这一点?
更改“createReader”,以便传入一个在图像可用时调用的处理函数:
function createReader(file, whenReady) {
reader.onload = function(evt) {
var image = new Image();
image.onload = function(evt) {
var width = this.width;
var height = this.height;
if (whenReady) whenReady(width, height);
};
image.src = evt.target.result;
};
reader.readAsDataURL(file);
}
现在,当您调用它时,您可以传入一个函数来根据图像尺寸执行您想要执行的操作:
createReader(input.files[i], function(w, h) {
alert("Hi the width is " + w + " and the height is " + h);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)