当前位置 : 主页 > 网络编程 > JavaScript >

js获取图片高度

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt window.onload = function() {var fileInput = document.getElementById('fileInput');var fileDisplayArea = document.getElementById('fileDisplayArea');fileInput.addEventListener('change', function(e) {var file = fileInput.files[0];
gistfile1.txt
window.onload = function() {

		var fileInput = document.getElementById('fileInput');
		var fileDisplayArea = document.getElementById('fileDisplayArea');


		fileInput.addEventListener('change', function(e) {
			var file = fileInput.files[0];
			var imageType = /image.*/;

			if (file.type.match(imageType)) {
				var reader = new FileReader();

				reader.onload = function(e) {
					fileDisplayArea.innerHTML = "";

					var img = new Image();
        $(img).css('display','none');
  
					img.src = reader.result;
       img.onload = function() {
          console.log(img.height);
          console.log(img.width); // image is loaded; sizes are available
       };

					
      $('body').append(img);
          
				}

				reader.readAsDataURL(file);	
			} else {
				fileDisplayArea.innerHTML = "File not supported!"
			}
		});

}
gistfile2.txt
 
	
  

Image File Reader

Select an image file:
网友评论