简体   繁体   English

在Javascript中获取图像变量的高度和宽度

[英]Getting height and width of an image variable in Javascript

I'm trying to load a picture in an image variable and test whether it is landscape or portrait for putting it in a frame. 我正在尝试将图片加载到图像变量中,并测试将其放置在框架中是横向还是纵向。 The user selects the URL from a drop-down. 用户从下拉列表中选择URL。 But, the following snippet gives a width of 0 about every other time. 但是,以下代码段几乎每隔一次给出0的宽度。 What dumb thing am I doing? 我在做什么傻事?

        var Select_Picture = document.getElementById("ddlSelectPicture");
        var Client_Picture = document.getElementById("imgClientPicture");
        var Temp_Image = new Image();
        var Image_Height;
        var Image_Width;

        Temp_Image.src = Select_Picture.value;

        // WAIT FOR PICTURE TO LOAD
        while (Temp_Image.width < 1 || Temp_Image.height < 1)
        {
            Image_Width = Temp_Image.width;
        }
        Image_Height = Temp_Image.height;

        alert(Image_Width + "  " + Image_Height);

You are trying to read the height and width of the image before it has loaded. 您正在尝试读取图像的高度和宽度,然后再加载图像。
onload will be called when Temp_Image has finished loading, so the image will have a width and height. Temp_Image完成加载后将调用onload ,因此图像将具有宽度和高度。

Temp_Image.onload = function() {
    console.log(Temp_Image.width);
}
Temp_Image.src = Select_Picture.value;

You can directly get the height and width like this: 您可以像这样直接获得高度和宽度:

var height = document.getElementById("myImg").height;
var width = document.getElementById("myImg").width;

Even though javascript is single threaded, the browser is not. 即使javascript是单线程的,浏览器也不是。 So while you do your best to bring your user's system to a screeching halt with your endless loop, the browser still manages to load your image and now needs to push it's properties out into the javascript world for you to access. 因此,尽管您尽力使您的用户系统陷入无休止的循环,但浏览器仍然设法加载图像,现在需要将其属性推送到javascript世界中,以供您访问。

Your while loop will keep jumping in and getting in the way of everything javascript related that the browser tries to do, so even after the image is ready, it may take many cycles of the event loop to actually populate the properties of width and height on your image and they likely won't update in the same loop. 您的while循环将不断跳入并妨碍浏览器尝试执行的所有与javascript相关的操作,因此,即使在图像准备就绪后,可能仍需要花费很多周期的事件循环才能真正填充上的widthheight属性您的图片,它们可能不会在同一循环中更新。

So your test is going like this: (we'll pretend it's 100px x 100px) 所以您的测试是这样的:(我们假设它是100px x 100px)

// repeat this over and over and over
if ( Temp_Image.width < 1) /* true */
   Image_Width = Temp_Image.width /* 0 */

// until finally
if (Temp_Image.width === 0 /* false */)
    // okay we finally branched into right side of expression
    if (Temp_Image.height < 1) /* ??? */
        // what now?

Well if its true (meaning height not yet available), it will look like this. 好吧,如果它是真实的(意味着尚未提供高度),它将看起来像这样。

// keep repeating this over and over and over
if (Temp_Image.width < 1 ) /* false */ 
    if (Temp_Image.height < 1) /* true */
       Image_Width = Temp_Image.width /* 100 */

// until finally
if (Temp_Image.width < 1 ) /* false */
    if (Temp_Image.height < 1) /* false */
        break
Image_Height = Temp_Image.height /* 100 */

// we've finally left the loop and the width is properly set

Well if its false (meaning height already available), it will look like this. 好吧,如果它是错误的(意味着高度已经可用),它将看起来像这样。

if (Temp_Image.width < 1 ) /* false */
    break
Image_Height = Temp_Image.height /* 100 */

// wait... hold on... we forgot to set the width

You set the width to 0 over and over on every iteration of the loop, but if the height property is already available when you finally receive a real width value, then you break at the top of the loop before stashing it in a variable. 您可以在循环的每次迭代中将宽度反复设置为0,但是如果在最终收到实际宽度值时height属性已经可用,则在将其存储到变量中之前先在循环的顶部中断。

There is no guarantee of which property is set first, (width or height), as so clearly demonstrated by your 50/50 results. 无法保证先设置哪个属性(宽度或高度),如您的50/50结果所清楚显示的那样。

So if this is for studying loops, then go ahead and try to make it work properly yourself. 因此,如果这是为了研究循环,那么请继续尝试自己使之正常工作。 Might be a good exercise if the answer doesn't seem obvious to you. 如果答案对您而言似乎并不明显,则可能是一个很好的练习。

But if you are actually trying to serve images in the browser, then DON'T DO THAT! 但是,如果您实际上是在尝试在浏览器中提供图片,那就不要这样做! You're blocking the thread! 您正在阻塞线程! Stop it! 停下来!

Follow the already given advice of listening for the load event instead looping. 遵循已经给出的监听load事件而不是循环的建议。

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

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