Javascript - 获取图像高度 [英] Javascript - Get Image height
问题描述
我需要使用 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>
谢谢,
比诺德·苏曼
http://binodsuman.blogspot.com/2009/06/how-to-get-height-and-widht-of-image.html
这篇关于Javascript - 获取图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!