Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 624
cicakman Avatar asked Aug 29 '10 14:08

cicakman


2 Answers

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.

like image 154
Pointy Avatar answered Nov 05 '22 08:11

Pointy


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.

like image 41
user113716 Avatar answered Nov 05 '22 09:11

user113716