JS - get image width and height from the base64 code

后端 未结 4 697
攒了一身酷
攒了一身酷 2020-11-27 14:46

I have a base64 img encoded that you can find here. How can I get the height and the width of it?

4条回答
  •  执笔经年
    2020-11-27 15:29

    Create a hidden with that image and then use jquery .width() and . height()

    $("body").append("");
    var width = $('#hiddenImage').width();
    var height = $('#hiddenImage').height();
    $('#hiddenImage').remove();
    alert("width:"+width+" height:"+height);
    

    Test here: FIDDLE

    Image is not initially created hidden. it gets created, then you get width and height and then remove it. This may cause a very short visibility in large images in this case you have to wrap the image in another container and make that container hidden not the image itself.


    Another Fiddle that does not add to dom as per gp.'s anser : HERE

提交回复
热议问题