tags:

views:

32

answers:

2

There are several images in folder . I want to fit as many images as possible from the folder into an html page. The images will be selected at random and will have different width & height. I don't want the dimension of the images to change.

How can this be accomplished ?? is this doable with JavaScript ? This looks pretty complicated to me . is there a simple way of doing this via some other method (server side)?

thanks

Edit: so basically we have a room in which we want to pack as many boxes as we can , the boxes all have different size and cannot be stacked over one another..

A: 

It would be doable but more difficult I think in JavaScript,

You could use a server side language like PHP to do it like this:

  1. load random image files
  2. add up their widths and stop adding images after you've reached your desired page width (do this for height too)
  3. let PHP echo out the images as html <img/> elements with a basic html layout surrounding it or an empty layout

If you could elaborate some more about the details of the page (kind of layout , etc.) I might be able to provide a more specific answer.

Hope this helps.

Update:

It's quite easy to do in php, if you would do it in javascript I would imagine the approach being something like this:

  1. page with <img width="" height =""/> elements
  2. using the DOM functions to get the elements and iterate through them to make a list while you are still below your maximum width and height
  3. place them next to each other with CSS positioning
Mervin
The idea is just in my mind right now. so have no details about layout.
I've updated my answer.
Mervin
I don't see any benefit from using PHP. If you are just putting images on the page randomly, you can do that in plain HTML. Just put images one after each other, and the browser automatically wraps to next line when needed. But I guess the question was about optimizing the layout, which is much more complicated.
PauliL
@Paulil: different browsers and devices handle that behaviour differently which can be difficult since it may provide various results.
Mervin
+1  A: 

This sounds like the bin packing problem:

http://en.wikipedia.org/wiki/Bin_packing_problem

http://mathworld.wolfram.com/Bin-PackingProblem.html

dvcolgan