## Creating a perfect grid of images

Hello everyone,

Since a long time I wanted to work on a perfect grid of images, something I haven't saw anywhere yet.

I need an image gallery that would do the following:

1) Create a perfect grid from inputted images of any size and any amount with no gaps at all.
2) In order to fill gaps in, the system can resize the images, however ratio should be kept (In case there will be still gaps, the system can also crop certain images).
3) Images should not be much different in sizes, that means if we got 2 inputted images, 1st is 3000x2000 and the 2nd is 600x300, then obviously the first image should be resized smaller.
4) Lets say that for the start, the canvas that holds all the images will be fluid, but should be a square canvas (or at least close to be a square canvas, what I mean is that it shouldn't be 1000x200 but more like 800x600 etc).

There are probably more parameters to be taken care of, but these are the main ones.
Basically, I need it done with PHP GD library (or imagick) but coding it in any other language would work, as I just need to see the calculations that needs to be taken in order to achieve such a thing.
Please try to avoid posting links to resources that do similar things, as its only similar and not excactly what I need (feel free to post links to resources that could help get to the solution however), here are a couple of resources I looked at and can be helpful at starting it at least:

Mason.js: http://masonjs.com/
Problem: I don't see how does it fit with images at all, It doesn't care about images sizes so it can put wide images in high squares and it duplicates images to fill in empty spaces, it got no calculations at all for resizing elements in order to fit in.

Isotope.js: http://isotope.metafizzy.co/
Problem: There are gaps between elements and I doubt there are any resizing calculations for images here either, to be honest I liked Mason.js more.

Feel free to discuss the issue and possible solutions, I would really like to see where it would go and if we will be able to build something that would achieve this.

Regards,
Ulthane.