简体   繁体   English

如何使用javascript从文件获取图像尺寸(高度,宽度)

[英]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.

相关问题 如何使用javascript或jquery获取图像的自然尺寸? - How do I get natural dimensions of an image using javascript or jquery? 如何从 javascript 中的 object 获取宽度和高度值 - how do I get the width and height values from this object in javascript 如何从JavaScript中的Base64图像获取图像尺寸? - How do I get image dimensions from a Base64 Image in JavaScript? 在IE7,8上,选择文件后如何使用javascript获取图像的宽度,高度和文件类型? - How to get image width , height, and file type using javascript On IE7, 8 , After select file? 如何获取存储在javascript对象中的图像的宽度和高度? - How do I get the width and height of an image stored inside a javascript object? 如何在 Javascript 中收到图像尺寸可用的警报? - How do I get alerted in Javascript that an image's dimensions are available? 获取图像尺寸(高度和宽度)Js 或 jquery - Get Image Dimensions (Height and Width) Js or jquery 如何使用 JavaScript 获取图像尺寸(高度和宽度)? - How to get image size (height & width) using JavaScript? JavaScript如何从XMLHttpRequest获取图像的宽度和高度 - JavaScript How to get an image width and height from an XMLHttpRequest KineticJS:如何获取Sprite或图片的宽度和高度? - KineticJS: How do I get the Width and Height of a Sprite or Image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM