Find the dimensions of an image from bulk URLS

Hello friends of StackOverflow!

I have a txt file with hundreds of web URLS that point to images. The images are all in JPG form.

I am trying to find out the dimensions of all the images in bulk without having to go through and inspect element on every single one of them. That would take hours. I thought about creating some sort of loop in javascript that reads the lines one by one but I am stumped. I do have a code that tells me the image dimensions but it doesn't perform the operation in bulk. I have to replace the URL of the image every time.

How would I go about this? It would be ideal if I had a piece of code (no language preference) that can read the txt file line by line and write the image dimensions corresponding to the line read in a new text file.

Here is my code so far: https://codepen.io/th3pr099/pen/XGVoMp

function getMeta(url, callback) {
var img = new Image();
img.src = url;
img.onload = function() { callback(this.width, this.height); }
}
getMeta(
"https://www.w3schools.com/w3css/img_lights.jpg",
function(width, height) { alert("Width: " + width + 'px ' + "Height: " 
+ height + 'px') }
);

Thank you so much for your help!

728x90

1 Answers Find the dimensions of an image from bulk URLS

Here's an idea, written in JavaScript (notice that it uses your getMeta function.):

<!doctype html>
<html>
  <body>
    Below is the textarea element that will take in the contents of your text file:<br />
    <textarea id="txtarea"></textarea>
    <button id="btn">Get image dimensions</button><br/><br/><br/>
    <script>
      //Your `getMeta` function goes below (I found that it works great)
      function getMeta(url, callback) {
      var img = new Image();
      img.src = url;
      img.onload = function() { callback(this.width, this.height); }
      }

      //When the button (id of "btn") is pressed,
      document.getElementById("btn").onclick=function() {
        //Get the value of the textarea, then split it on newlines
        var listOfLinks=document.getElementById("txtarea").value.split("\n");
        //iterate through each line
        for (var i=0;i<listOfLinks.length;i++) {
          //run your `getMeta` function to get width and height
          getMeta(listOfLinks[i],function(w,h) {
            //Append data to body
            document.body.innerHTML+="Image Dementions: ("+w+","+h+")<br />";
          });
        }
      };
    </script>
  </body>
</html>

6 days ago