Get width height of remote image from url
Get image size with JavaScript
In order to read the data from an image you'll need to make sure it's first loaded. Here's a callback-based approach and two promise-based solutions:
Callback
const getMeta = (url, cb) => {
const img = new Image();
img.onload = () => cb(null, img);
img.onerror = (err) => cb(err);
img.src = url;
};
// Use like:
getMeta("https://i.stack.imgur.com/qCWYU.jpg", (err, img) => {
console.log(img.naturalWidth, img.naturalHeight);
});
Get the real width and height of an image with JavaScript? (in Safari/Chrome)
Webkit browsers set the height and width property after the image is loaded. Instead of using timeouts, I'd recommend using an image's onload event. Here's a quick example:
var img = $("img")[0]; // Get my img elem
var pic_real_width, pic_real_height;
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
pic_real_width = this.width; // Note: $(this).width() will not
pic_real_height = this.height; // work for in memory images.
});
To avoid any of the effects CSS might have on the image's dimensions, the code above makes an in memory copy of the image. This is a very clever solution suggested by FDisk.
You can also use the naturalHeight
and naturalWidth
HTML5 attributes.
Javascript, how can I get image width and height from a blob?
You can use the Image constructor
const img = new Image();
img.src = imageDataUrl;
img.onload = () => {
// img.width
// img.height
};
Get height/width of image in Javascript (ideally without loading the image at all)
If the image is not loaded, it won't have its' height and width set. You have to wait until the image is fully loaded, then inspect its' size. Maybe something along these lines:
function getWidthAndHeight() {
alert("'" + this.name + "' is " + this.width + " by " + this.height + " pixels in size.");
return true;
}
function loadFailure() {
alert("'" + this.name + "' failed to load.");
return true;
}
var myImage = new Image();
myImage.name = "image.jpg";
myImage.onload = getWidthAndHeight;
myImage.onerror = loadFailure;
myImage.src = "image.jpg";
Can't get height and width of image in javascript
you should get size after image loaded:
img.onload = getSize;//measure after loading or you will get 0
How to get Image height within the div in javascript or jquery?
Just Use .height()
method
var imageheight = $(".imgclass img").height();alert(imageheight);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="imgclass"><img src="http://tympanus.net/Tutorials/CaptionHoverEffects/images/1.png"></div>
Related Topics
Prevent Automatic Browser Scroll on Refresh
Typescript Compile to Single File
Jquery's Live() Is Deprecated. What Do I Use Now
How to Populate the Options of a Select Element in JavaScript
Downloading Canvas Element to an Image
Create a File Using JavaScript in Chrome on Client Side
Get Function Name in JavaScript
How to Filter by Object Property in Angularjs
JSON Stringify Changes Time of Date Because of Utc
Calling Method Using JavaScript Prototype
Why Not Always Use the Index as the Key in a Vue.Js for Loop
JavaScript Getelementbyid Based on a Partial String