最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remote image properties using jquery - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Oct 31, 2011 at 11:44 Kalle H. Väravas 3,6154 gold badges31 silver badges47 bronze badges asked Aug 29, 2010 at 14:28 cicakmancicakman 1,9905 gold badges22 silver badges34 bronze badges 1
  • Just out of curiosity, how far have you gone with this link sharing code. im trying to find a jquery plugin out there that can do this.... – Sir Lojik Commented Jan 4, 2011 at 9:03
Add a ment  | 

2 Answers 2

Reset to default 13

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.

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/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.

发布评论

评论列表(0)

  1. 暂无评论