JustifiedJS is a wonderful JavaScript library, which allows you to arrange your image gallery to fit into a square frame, while keeping their ratios intact. The library comes with many options including the image path to retrieve different sized images; it serves the smallest image when required.