[英]How do i get the image dimensions (height, width) from a file by using javascript
I am probably being very very stupid. 我可能非常非常愚蠢。 But i cant get this to work for my life.
但是我不能让它为我的生活工作。 I am trying to set the
imgWidth
to the same width as the image width of the file (pic_real_width). 我正在尝试将
imgWidth
设置为与文件的图像宽度(pic_real_width)相同的宽度。
function calculate() {
var pic_real_width, pic_real_height;
$("<img/>").attr("src", $("#pp_photo_popUp")[0].src).load(function() {
pic_real_width = this.width; // Note: $(this).width() will not
pic_real_height = this.height; // work for in memory images.
});
var imgWidth =
}
All in all I am trying to make a function to get the real dimensions of a file on the hard disk. 总之,我正在尝试创建一个函数来获取硬盘上文件的实际尺寸。
Bind the load event First, and then do all processing that needs the image to be loaded inside of the load event. 绑定加载事件首先,然后执行所有需要在加载事件中加载图像的处理。
function calculate(callback) {
$("<img/>").load(function() {
var pic_real_width = this.width; // Note: $(this).width() will not
var pic_real_height = this.height; // work for in memory images.
callback(pic_real_width,pic_real_height);
}).attr("src", $("#pp_photo_popUp")[0].src);
}
calculate(function(width,height) {
console.log(width,height);
});
If you set the src first, some versions of IE will immediately trigger the load event synchronously, thus triggering it before you have even bound to it. 如果首先设置src,则IE的某些版本将立即同步触发load事件,从而在绑定到该事件之前就触发了该事件。 That's why i swapped the order.
这就是为什么我调换订单。 Additionally, I added the callback parameter because I assume later in the function you were going to return the width/height, which won't work due to the asynchronous nature of load events.
另外,我添加了回调参数,因为我假设在函数的后面,您将返回宽度/高度,由于加载事件的异步特性,该宽度/高度将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.