jQuery.naturalWidth / jQuery.naturalHeight images natural Width Height Native Width (ok)

Lấy kích thước ảnh gốc

https://gist.github.com/johan/2209957

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- Latest compiled and minified CSS & JS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery.js"></script>
  <style>
    #imgsize >img {
			width: 400px;
			margin: 0 auto;
			display: block;
		}
	</style>
</head>
<body>
  <div id="imgsize">
    <img src="rose.jpg" />
  </div>
  <script type="text/javascript">
	  (function($) {
	    function img(url) {
	      var i = new Image;
	      i.src = url;
	      return i;
	    }
	    if ('naturalWidth' in (new Image)) {
	      $.fn.naturalWidth = function() { 
	      	return this[0].naturalWidth; 
	      };
	      $.fn.naturalHeight = function() { 
	      	return this[0].naturalHeight; 
	      };
	      return;
	    }
	    $.fn.naturalWidth = function() { 
	    	return img(this.src).width; 
	    };
	    $.fn.naturalHeight = function() { 
	    	return img(this.src).height; 
	    };
	  })(jQuery);
	  var pi = $('#imgsize img').prop('naturalWidth');
	  console.log(pi);
  </script>
</body>
</html>

Hoặc có thể sử dụng cách ngắn gọn hơn :)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- Latest compiled and minified CSS & JS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery.js"></script>
  <style>
    #imgsize >img {
			width: 400px;
			margin: 0 auto;
			display: block;
		}
	</style>
</head>
<body>
  <div id="imgsize">
    <img src="rose.jpg" />
  </div>
  <script type="text/javascript">
	  // Get on screen image
		var screenImage = $("#imgsize img");
		// Create new offscreen image to test
		var theImage = new Image();
		theImage.src = screenImage.attr("src");
		// Get accurate measurements from that.
		var imageWidth = theImage.width;
		var imageHeight = theImage.height;
	  // var pi = $('#imgsize img').prop('naturalWidth');
	  console.log(imageWidth);
  </script>
</body>
</html>

Hoặc sử lý nhiều ảnh :)

<script type="text/javascript">
						jQuery(document).ready(function($) {
						  var theImageCr = new Image();
						  $("body.single.single-listing .pflist-imagecontainer .pfuorgcontainer>img").each(function(){
								theImageCr.src = $(this).attr("src");
							  var imageWidthCr = theImageCr.width;
								var imageHeightCr = theImageCr.height;
								console.log(imageWidthCr);
	        			if (imageWidthCr < 260 || imageHeightCr < 209) {
			            $(this).attr("src", "https://app.yp.vn/wp-content/plugins/pointfindercoreelements/public/images/noimg.png");
			          }
							});
						});
						</script>

Last updated

Navigation

Lionel

@Copyright 2023