get image size from url javascript code example

Example 1: js get image from url

var elements = [];
function loaded(){
  console.log("you have loaded an image");
}
CreateFileFrom("your/dir/here/img.png");

function CreateFileFrom(dir){

  		/* defining runtime variables */
	  		var extension                        =   dir.split('.').pop();
	  		var keys                             =   {"png":"IMG","jpg":"IMG","jpeg":"IMG",
	  												  "js":"SCRIPT","json":"SCRIPT",
	  												  "mp3":"AUDIO","wav":"AUDIO"};
	  	    var obj                              =   document.createElement(keys[extension]) || {};
	  	    obj.src                              =   dir;
	  	    
	  	/* onload function called when the resource is loaded */
	  	    obj.onload                           =   (e) => {

	  	    	elements.push(e.path[0]);
	  	    	loaded()
	  		}

  		/* make sure that the data is compitable */
  			if(keys[extension]==null){console.error("not supported media type "+extension);return;}
  	}

Example 2: get image file width javascript

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Get image width using File API</title>
  </head>

  <body>
    <input type='file' onchange="readURL(this);" />

    <script>
      function readURL(input) {
        if (input.files && input.files[0]) {
          var reader = new FileReader();
          reader.onload = function (e) {
            var img = new Image;
            img.onload = function() {
              console.log("The width of the image is " + img.width + "px.");
            };
            img.src = reader.result;
          };
          reader.readAsDataURL(input.files[0]);
        }
      }
    </script>
  </body>
</html>