Get the real width and height of an image with JavaScript? (in Safari/Chrome)

后端 未结 30 2786
傲寒
傲寒 2020-11-22 01:16

I am creating a jQuery plugin.

How do I get the real image width and height with Javascript in Safari?

The following works with Firefox 3, IE7 and Opera 9:

30条回答
  •  眼角桃花
    2020-11-22 01:40

    I use different approach, simply make Ajax call to server to get image size when image object is in use.

    //make json call to server to get image size
    $.getJSON("http://server/getimagesize.php",
    {"src":url},
    SetImageWidth
    );
    
    //callback function
    function SetImageWidth(data) {
    
        var wrap = $("div#image_gallery #image_wrap");
    
        //remove height
         wrap.find("img").removeAttr('height');
        //remove height
         wrap.find("img").removeAttr('width');
    
        //set image width
        if (data.width > 635) {
            wrap.find("img").width(635);
        }
        else {
             wrap.find("img").width(data.width);
        }
    }
    

    and of course server side code:

    $image_width,'height'=>$image_height);
    
    echo json_encode($arr);
    
    ?>
    

提交回复
热议问题