[removed] Get image dimensions

后端 未结 8 1659
攒了一身酷
攒了一身酷 2020-11-29 19:11

I only have a URL to an image. I need to determine the height and width of this image using only JavaScript. The image cannot be visible to the user on the page. How can I g

相关标签:
8条回答
  • 2020-11-29 20:02

    if you have image file from your input form. you can use like this

    let images = new Image();
    images.onload = () => {
     console.log("Image Size", images.width, images.height)
    }
    images.onerror = () => result(true);
    
    let fileReader = new FileReader();
    fileReader.onload = () => images.src = fileReader.result;
    fileReader.onerror = () => result(false);
    if (fileTarget) {
       fileReader.readAsDataURL(fileTarget);
    }
    
    0 讨论(0)
  • 2020-11-29 20:05

    Similar question asked and answered using JQuery here:

    Get width height of remote image from url

    function getMeta(url){
      $("<img/>").attr("src", url).load(function(){
         s = {w:this.width, h:this.height};
         alert(s.w+' '+s.h);      
      }); 
    }
    
    getMeta("http://page.com/img.jpg");
    
    0 讨论(0)
提交回复
热议问题