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";
Get width height of remote image from url
Get image size with jQuery
function getMeta(url) {
$("<img/>",{
load: function() {
alert( this.width +" "+ this.height );
},
src: url
});
}
Get image size with JavaScript
function getMeta(url) {
var img = new Image();
img.onload = function() {
alert( this.width +" "+ this.height );
};
img.src = url;
}
Get image size with JavaScript (modern browsers, IE9+ )
function getMeta(url){
const img = new Image();
img.addEventListener("load", function() {
alert( this.naturalWidth +' '+ this.naturalHeight );
});
img.src = url;
}
Use like: getMeta( "http://example.com/img.jpg" );
https://developer.mozilla.org/en/docs/Web/API/HTMLImageElement
Getting Image Dimensions using Javascript File API
Yes, read the file as a data URL and pass that data URL to the src
of an Image
: http://jsfiddle.net/pimvdb/eD2Ez/2/.
var fr = new FileReader;
fr.onload = function() { // file is loaded
var img = new Image;
img.onload = function() {
alert(img.width); // image is loaded; sizes are available
};
img.src = fr.result; // is the data URL because called with readAsDataURL
};
fr.readAsDataURL(this.files[0]); // I'm using a <input type="file"> for demonstrating
How do I get natural dimensions of an image using JavaScript or jQuery?
You could use naturalWidth
and naturalHeight
, these properties contain the actual, non-modified width and height of the image, but you have to wait until the image has loaded to get them
var img = document.getElementById('draggable');
img.onload = function() {
var width = img.naturalWidth;
var height = img.naturalHeight;
}
This is only supported from IE9 and up, if you have to support older browser you could create a new image, set it's source to the same image, and if you don't modify the size of the image, it will return the images natural size, as that would be the default when no other size is given
var img = document.getElementById('draggable'),
new_img = new Image();
new_img.onload = function() {
var width = this.width,
heigth = this.height;
}
new_img.src = img.src;
FIDDLE
HTML5 - how to get image dimension
Change "createReader" so that you pass in a handler function to be called when the image is available:
function createReader(file, whenReady) {
reader.onload = function(evt) {
var image = new Image();
image.onload = function(evt) {
var width = this.width;
var height = this.height;
if (whenReady) whenReady(width, height);
};
image.src = evt.target.result;
};
reader.readAsDataURL(file);
}
Now when you call it, you can pass in a function to do whatever it is you want done with the image dimensions:
createReader(input.files[i], function(w, h) {
alert("Hi the width is " + w + " and the height is " + h);
});
Is it possible to check dimensions of image before uploading?
You could check them before submitting form:
window.URL = window.URL || window.webkitURL;
$("form").submit( function( e ) {
var form = this;
e.preventDefault(); //Stop the submit for now
//Replace with your selector to find the file input in your form
var fileInput = $(this).find("input[type=file]")[0],
file = fileInput.files && fileInput.files[0];
if( file ) {
var img = new Image();
img.src = window.URL.createObjectURL( file );
img.onload = function() {
var width = img.naturalWidth,
height = img.naturalHeight;
window.URL.revokeObjectURL( img.src );
if( width == 400 && height == 300 ) {
form.submit();
}
else {
//fail
}
};
}
else { //No file was input or browser doesn't support client side reading
form.submit();
}
});
This only works on modern browsers so you still have to check the dimensions on server side. You also can't trust
the client so that's another reason you must check them server side anyway.
Related Topics
How to Detect If Flash Is Installed and If Not, Display a Hidden Div That Informs the User
Check If Url Scheme Is Supported in JavaScript
Converting Any String into Camel Case
Window.Open and Pass Parameters by Post Method
How to Extend a Class Without Having to Use Super in Es6
How to Match Multiple Occurrences with a Regex in JavaScript Similar to PHP's Preg_Match_All()
JavaScript Validation for Empty Input Field
How to Wait for a JavaScript Promise to Resolve Before Resuming Function
Check If Event Is Triggered by a Human
Track Event in Google Analytics Upon Clicking Form Submit
What's the Fastest Way to Convert String to Number in JavaScript
How to Programmatically Force an Onchange Event on an Input
How to Check If the Array of Objects Have Duplicate Property Values
How to Read from Chrome's Console in JavaScript
Jquery Equivalent of Getting the Context of a Canvas
Shrink Div to Text That's Wrapped to Its Max-Width