I have an array of scraped picture urls (So the pictures are still on the remove server). What I would like to do is check the dimensions of every picture and load only bigger ones (certain width/height) in the browser. I think I have to load the pictures first before checking the size, but how would you implement this that not all are shown but only bigger ones? Is this even possible?
-
Is the array of urls server side, or in Javascript in the browser? – Orbling Nov 24 '10 at 18:24
-
the array is server side. When asking the question i planned to send the array to the client and check the images there, but an answer below got me thinking if its not better to load and check the images server-side and send only the right ones to the client, what would you do ? (assuming im talking about 20 images) – ibmkahm Nov 25 '10 at 09:50
5 Answers
Assuming your Array is Server-Side and in PHP
You can get image sizes with the php function getimagesize()
which will return an array, containing the height and width (along with some other meta information). If the height and width match your criteria, you can add them to an output array that you can convert to json and send back to your jQuery script.
Assuming the image-references are in Javascript
You'll have to actually load the images to determine their dimensions. They don't have to be visible though. You can check their width by using the $.width()
method and their height by using the $.height()
method. If the dimensions meant your criteria, show the image.
$(function(){
var images = ['image1.jpg', 'image2.jpg'];
$(images).each(function(){
// Create our Image
currImage = document.createElement('img');
// Add it and hide it
$("body").append( $(currImage).attr('src', this).hide() );
// Determine whether it should stay
(currImage.width < 200) ? $(currImage).remove() : $(currImage).show() ;
});
});
-
thanks for the answer ! my array is server-side (but in asp.net). The php function getimagesize() probably loads the image from the remote server to my server to determine the dimensions right ? So assuming i want to check 20 pictures for their dimensions and show about 10 in the browser, what would you say is better performance wise: a) Load and check the pictures server-side and send the ones to show to the client or b) load every picture client-wise, check the dimensions with jquery/javascript and only show the right ones ?? – ibmkahm Nov 25 '10 at 09:44
-
One thing you need to be clear on is: performance-wise for who? You could indeed open a stream on the server and use the System.Drawing namespace to get image sizes but that will be using up YOUR server bandwidth/resources (and caching would be essential). If you do it in JavaScript, it's the user's browser that gets the image so it's their bandwidth... – Basic Nov 25 '10 at 10:02
-
@Basiclife: True... so i think i will go with the client-side version, thanks for the answer – ibmkahm Nov 25 '10 at 11:35
-
this soltion by Jonathan Sampson works only sometimes, probably for the reason Orbling explained in a post above (check "complete flag" bevore binding load event). Could somebody help me to add this in this solution ? – ibmkahm Nov 28 '10 at 16:28
You definitely need to load them all first, there's no way to get the width/height without doing so. However, you can check the dimensions before actually displaying them:
var img = document.createElement('img');
img.onload = function() {
if (img.width > ... && img.height > ...)
document.getElementById('some_container').appendChild(img);
else
img = null;
};
img.src = 'filename.jpg';
// update as per @Orbling's comment:
if (img.complete)
img.onload();

- 69,683
- 7
- 133
- 150
-
Note that if the image is cached, the onload event will not fire in some browsers, so it is best to check the `.complete` flag of the image after setting the source before binding the `.load()` event. See the accepted answer here: http://stackoverflow.com/questions/1114299/jquery-img-load-question – Orbling Nov 24 '10 at 18:32
-
this solution works, but only for one ímage, i couldnt get it to work for many pictures, where the image urls are stored in an array... i tried to use a for..in loop, but only the last image is shown – ibmkahm Nov 28 '10 at 16:30
-
@ibmkahm: That's because the `img` variable is actually local to the enclosing function and not local to the `for` loop, so the same variable is reused for all images. Try putting the image loading code in a separate function and call this function inside your loop. – casablanca Nov 28 '10 at 16:33
-
i still cant get it to work.... i dont know how to tell the onload handler what image he is dealing with. A global variable doesnt work because its overwritten bevore the onload handler is called. I tried many things but just couldnt get it to work, my best solution is in my beggining post, but this doesnt really work either. Would be really nice if somebody helps me to get casablancas solution to work with many images – ibmkahm Nov 29 '10 at 18:25
Another possibility would be to create new Image()
objects and set their sources to be each of the urls in the array. attach an onload
handler to the image and query its size before deciding whether or not to inject it into the document body.
I don't believe you'll be able to do this reliably without retrieving ALL the images in full from the server but you may be able to do AJAX HEAD requests to get a pictures filesize - assuming the server(s) in question respond as appropriate. This would allow you to filter out pictures which are obviously too small.

- 26,321
- 24
- 115
- 201
-
2A note of warning with this approach; if the images are already cached, the load event won't be fired. – Klemen Slavič Nov 24 '10 at 18:30
-
the best in my case would probably be to get only the picture header information from the remote server to my server (server-wise), read the size information, determine which images i want and load only those client-side... i will try to implement this sometime but it will probably a lot of work: getting header information for different picture formats (jpg, gif, png..) from the remote server and extrating size informations for each... for now i will just impement the client side version with jquery – ibmkahm Nov 25 '10 at 11:45
-
To clarify, I meant sending a `HEAD` request instead of a `GET` which returns the file size (in bytes), I hadn't anticipated downloading and interpreting the header bytes of the file itself which would be complex – Basic Nov 25 '10 at 12:49
I think after getting the images you can loop on them and get width and hight of each one and by that you can get which is bigger than others.
These questions answers may help you: Get the real width and height of an image with JavaScript? (in Safari/Chrome)

- 1
- 1

- 66,568
- 69
- 184
- 301
The best way to do this type of operation is to have some sort of web service that you can query to find the image sizes (or perhaps return the array, pre-sorted, with their image sizes) to the javascript.
A PHP page placed in the image directory can be called, and used to scan the images, sort the array by size, and return the array in JSON, XML, or HTML to your JS Code for displaying.

- 28,798
- 20
- 92
- 109