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);
});
Javascript: How to get width of image URL
If you append the image you create to the DOM then you should find that the width is then calculated and is then available as normal
var img = document.createElement("img");
img.src = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png";
img.onload = function(){
img.style.height = "200px";
img.style.visibility = 'hidden';
document.body.appendChild(img);
console.log(img.clientWidth);
}
How can i catch the width and height of a image link in angular?
Here is the javascript way :
function getImageDimenstion(imgUrl){
let img = new Image();
img.src = imgUrl; img.onload = function (event) { let loadedImage = event.currentTarget; let width = loadedImage.width; let height = loadedImage.height; console.log('height: '+height); console.log('width: '+width); } }
const url= "https://www.w3schools.com/bootstrap/paris.jpg";getImageDimenstion(url);
How to get image size from URL
Try this code it is working
var tmpImg = new Image();
tmpImg.src="https://www.google.com/intl/en_com/images/srpr/logo3w.png"; //or document.images[i].src;
$(tmpImg).one('load',function(){
orgWidth = tmpImg.width;
orgHeight = tmpImg.height;
alert(orgWidth+"x"+orgHeight);
});
The above code will gets the image dimension.For the file size:
No, there is no way you can get the image size using jQuery or pure JavaScript. The only way is to get it from server side using ajax.
You can get the image url send it to a service or server side page and have the page or service return the image size.
getting size of image from a link as dynamic content
I updated my answer. It now triggers on a mouseover event and doesn't use an ID. Be sure to accept the answer if this is working for you.
function getMeta(imageSrc,callback) { var img = new Image(); img.src = imageSrc; img.onload = function() { callback(this.width, this.height); }}
function hoverLink(imageSrc){ getMeta( imageSrc, function(width, height) { document.getElementById("prev").innerHTML = width + 'px ' + height + 'px'; } );}
function hoverOff(){ document.getElementById("prev").innerHTML =''; }
<a href="https://res.cloudinary.com/rss81/image/upload/gw.jpg" onmouseover="hoverLink(this.href)" onmouseout="hoverOff()"> Picture Link</a>
<div id='prev'></div>
Get the width and height of an image without downloading it
You will most likely not be able to do so without actually downloading the image (as in using the bandwidth).
That being said, you can use the FastImage gem to do this for you.
require 'fastimage'
FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56] # width, height
Get image dimensions from url path
I've used this library to perform the operation successfully.
Related Topics
How to Check If an Embedded Svg Document Is Loaded in an HTML Page
Getting a Union of Two Arrays in JavaScript
How to Read JSON File with Fetch() in JavaScript
When Do I Need to Specify the JavaScript Protocol
Why "This" Is Undefined Inside a Fat Arrow Function Definition
Understanding JavaScript Promises; Stacks and Chaining
Waiting for Image to Load in JavaScript
How to Read the Correct Time/Duration Values from Google Spreadsheet
In JavaScript, Can You Extend the Dom
How to Use Getelementsbyclassname in JavaScript-Function
How to Check "Checkbox" Dynamically - Jquery Mobile
React Doesn't Reload Component Data on Route Param Change or Query Change
Check If an Object Implements an Interface at Runtime with Typescript
How to Auto-Slide the Window Out from Behind Keyboard When Textinput Has Focus