cicakman
cicakman

Reputation: 1990

remote image properties using jquery

Currently i am trying to get remote image width/height. I am developing a link sharing module something like when you paste a link on facebook, you can see title, description and images.

So i tried using php getimagesize to get image width/height its very slow.

So i am thinking of using jquery solution to get remote image width/height so that i can filter image width less then 100px.

I am new in jquery/javascript

I tried something like

var img = $('#imageID');
var width = img.clientWidth;
var height = img.clientHeight;
$('#info').html(width+'.. height: '+height);

Its not working and return undefined .. height: undefined

Any help is appreciated.

Thank you

Upvotes: 7

Views: 4458

Answers (2)

Pointy
Pointy

Reputation: 413976

Try this:

var img = new Image();
img.src = 'http://your.url.here/image.png';
img.onload = function() {
  $('#info').text('height: ' + img.height + ' width: ' + img.width);
};

This approach would let you get the image info without having to have an <img> tag at all. Now, perhaps you want the image to be on the page, so you'd do what @patrick suggests in that case.

Upvotes: 13

user113716
user113716

Reputation: 322582

If you're trying to get the width and height of the image in the client side, you can use jQuery's .width() and .height() methods.

Example: http://jsfiddle.net/aeBWQ/

$(window).load(function() {
    var img = $('#imageID');
    var width = img.width();
    var height = img.height();
    $('#info').html(width+'.. height: '+height); 
});

Doing $(window).load() will ensure that the images are loaded before getting the height/width.

Upvotes: 2

Related Questions