简体   繁体   English

根据.load()中的宽度获取图像高度

[英]Get Image Height according to width in .load()

I need to get Image height and width before loading the Image itself. 在加载图像本身之前,我需要获取图像的高度和宽度。 So I am using jquery .load() method. 所以我正在使用jquery .load()方法。

$('<img />').attr('src',someURL).load(function(){
  console.log(this.width);
  console.log(this.height);
})

But problem is that I need the height according to the fix width. 但是问题是我需要根据固定宽度设置高度。 Suppose Image is of size 400*417, I need the height according to 200 not 400. For that I add 假设图像的大小为400 * 417,我需要根据200而不是400的高度。为此,我添加

$('<img />').attr({
'src':someURL,
'width':200}).load(function(){
  //Get height and width
})

But Above code is again giving me height 417 not according to width of 200. I am looking for js/jquery/angualrjs. 但是上面的代码再次给我417的身高,而不是200的宽度。我在找js / jquery / angualrjs。 Please help. 请帮忙。

Thanks In Advance :) 提前致谢 :)

A bit of math will do the trick. 一点数学就能解决问题。 Once you get the actual image dimensions, apply your ratio to get the height you want. 一旦获得了实际的图像尺寸,请应用您的比例来获得所需的高度。

For instance : 例如 :

$('<img />').attr('src',someURL).load(function(){
  console.log(this.width); // prints out 400
  console.log(this.height); // prints out 417

  var widthIWant = 200;

  var heightIWant = this.height * widthIWant / this.width; // Gives you 208.5

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM