Javascript - 获取图像高度 [英] Javascript - Get Image height

查看:91
本文介绍了Javascript - 获取图像高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用AJAX在网页上显示一堆图像。所有这些都有不同的尺寸,所以我想在显示它们之前调整它们的尺寸。有没有办法在JavaScript中执行此操作?

I need to display a bunch of images on a web page using AJAX. All of them have different dimensions, so I want to adjust their size before displaying them. Is there any way to do this in JavaScript?

对每个图像使用PHP的 getimagesize()会导致不必要的性能因为会有很多图像。

Using PHP's getimagesize() for each image causes an unnecessary performance hit since there will be many images.

推荐答案

我正在寻找一个解决方案,使用JavaScript获取图像的高度和宽度。我找到了很多,但所有这些解决方案只在图像存在于浏览器缓存中时才有效。

I was searching a solution to get height and width of an image using JavaScript. I found many, but all those solutions only worked when the image was present in browser cache.

最后我找到了一个解决方案来获取图像的高度和宽度,即使图像也是如此在浏览器缓存中不存在:

Finally I found a solution to get the image height and width even if the image does not exist in the browser cache:

<script type="text/javascript">

  var imgHeight;
  var imgWidth;

  function findHHandWW() {
    imgHeight = this.height;
    imgWidth = this.width;
    return true;
  }

  function showImage(imgPath) {
    var myImage = new Image();
    myImage.name = imgPath;
    myImage.onload = findHHandWW;
    myImage.src = imgPath;
  }
</script>

谢谢,

Binod Suman

Binod Suman

http://binodsuman.blogspot.com/2009/06/how-to-get-height-and-widht-of-image.html

这篇关于Javascript - 获取图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆