Getting image width on image load fails on IE

后端 未结 8 1354
自闭症患者
自闭症患者 2020-12-31 09:32

I have a image resizer function that resize images proportional. On every image load a call this function with image and resize if its width or height is bigger than my max

相关标签:
8条回答
  • 2020-12-31 10:11

    Don't use width and height. Use naturalWidth and naturalHeight instead. These provide the image unscaled pixel dimensions from the image file and will work across browsers.

    0 讨论(0)
  • 2020-12-31 10:12
        var screenW = screen.width;
        var screenH = screen.height;
        //alert( screenW );
        function checkFotoWidth( img, maxw )
        {
            if( maxw==undefined)
                maxw = 200;
            var imgW = GetImageWidth(img.src);
            var imgH = GetImageHeight(img.src);
                //alert(GetImageWidth(img.src).toString()); // img.width); // objToString(img));
            if (imgW > maxw || (img.style.cursor == "hand" && imgW == maxw))
            {
                if (imgW > screenW) winW = screenW;
                else winW = imgW;
    
                if (imgH > screenH) winH = screenH;
                else winH = imgH;
    
                img.width=maxw;
                img.style.cursor = "pointer";
    
                img.WinW = winW;
                img.WinH = winH;
                //alert("winW : " + img.WinW);
                img.onclick = function() { openCenteredWindow("Dialogs/ZoomWin.aspx?img=" + this.src, this.WinW, this.WinH, '', 'resizable=1'); }
                img.alt = "Klik voor een uitvergroting :: click to enlarge :: klicken Sie um das Bild zu vergrössern";
                //alert("adding onclick);
            }
        }
    
        function GetImageWidth(imgSrc) 
        {
            var img = new Image();
            img.src = imgSrc;
            return img.width;
        } 
    
        function GetImageHeight(imgSrc) 
        {
            var img = new Image();
            img.src = imgSrc;
            return img.height;
        } 
    
    0 讨论(0)
  • 2020-12-31 10:20

    This is how I solved it (because it's the only js on the site I didn't want to use a library).

        var imageElement = document.createElement('img');
        imageElement.src = el.href; // taken from a link cuz I want it to work even with no script
        imageElement.style.display      = 'none';
    
        var imageLoader = new Image();
        imageLoader.src = el.href;
        imageLoader.onload = function() {
            loaderElement.parentElement.removeChild(loaderElement);
            imageElement.style.position     = 'absolute';
            imageElement.style.top          = '50%';
            imageElement.style.left         = '50%';
            // here using the imageLoaders size instead of the imageElement..
            imageElement.style.marginTop    = '-' + (parseInt(imageLoader.height) / 2) + 'px';
            imageElement.style.marginLeft   = '-' + (parseInt(imageLoader.width) / 2) + 'px';
            imageElement.style.display      = 'block';
        }
    
    0 讨论(0)
  • 2020-12-31 10:22

    Try

     function onImageLoad(img, maxWidth, maxHeight) {
       var width = img.width; // Problem is in here
       var height = img.height // Problem is in here
       if (height==0  && img.complete){
           setTimeOut(function(){onImageLoad(img, maxWidth, maxHeight);},50);
       }
    
     }
    
    0 讨论(0)
  • 2020-12-31 10:30

    Man, I was looking for this for 2 days. Thanks.

    I'm using jQuery, but it doesnt matter. Problem is related to javascript in IE.

    My previous code:

    var parentItem = $('<div/>')
       .hide();      // sets display to 'none'
    
    var childImage = $('<img/>')
       .attr("src", src)
       .appendTo(parentItem)   // sets parent to image
       .load(function(){
          alert(this.width);  // at this point image is not displayed, because parents display parameter is set to 'none' - IE gives you value '0'
       });
    

    This is working in FF, Opera and Safari but no IE. I was getting '0' in IE.

    Workaround for me:

    var parentItem = $('<div/>')
       .hide();
    
    var childImage = $('<img/>')
       .attr("src", src)
       .load(function(){
          alert(this.width);  // at this point image css display is NOT 'none'  - IE gives you correct value
          childImage.appendTo(parentItem);   // sets parent to image
       });
    
    0 讨论(0)
  • 2020-12-31 10:30

    It's because IE can't calculate width and height of display: none images. Use visibility: hidden instead.

    0 讨论(0)
提交回复
热议问题