Layout Algorithms: Facebook | Google Plus | Lightbox | Lightbox Android | 500px

Google Plus has a really nice image gallery. They somehow managed to display all the photos without cropping, without reordering and without any holes. We are going to see how they did it in this blog post.

How does it work?

Here we have three images with various sizes and aspect ratio and we want to display them in the page. The layout algorithm is the consequence of one clever trick: all the images of the same row are have the same height.

So the only unknown is H, the height of the row given the three images we want to show. Using some basic math, we can solve the problem!

So now we know how to calculate H, the height of all the images. Since we want to keep aspect ratio, we can also calculate their width. As you can see, it is trivial to generalize the operation to n images.

How many images?

Now the tricky part is to decide how many images we want to put in the row. I came up with a solution that gives similar results to Google Plus but I'm not 100% sure that's how they do it.

One fact you can observe is the fact that few images leads to a huge row while many images lead to a small row.

So the idea is to try adding one image at a time and have a threshold for the maximum height we want. Once the row is smaller than this threshold, we render it!

Conclusion

Check out the Demo! Here's a little Pro/Con to know if this technique will fit your needs.

Pros:

  • No cropping
  • No reordering
  • No holes
  • Arbitrary Width

Cons:

  • Portrait images are much smaller than landscape ones
  • All the rows do not have the same height
  • The view feels a bit chaotic with no clear structure
  • Requires dynamic resize of images

Some other implementations I found on the internet:

If you liked this article, you might be interested in my Twitter feed as well.
 
  • Wcchok

    Do you need jquery plugin in your G+ example?

  • Vojtech Sefler

    Hi, if I try to put images into fixed-width div, your script doesn't work... http://sefler.cz/test/abc.htm How can I fix it please?

  • Ludovic

    NIce code but is it possible to add 10px margins between photographs like Google ?

  • http://www.facebook.com/vjeux Christopher Chedeau

    Yes you can, look at the demo to see how to do it: http://blog.vjeux.com/wp-content/uploads/2012/05/google-layout.html

  • Arnaud

    Merci pour l'article, très clair et lisible. Cela m'a pas mal aidé pour un algorithme de création de mur de photos.

    Continues comme ça !

  • http://twitter.com/mhm72 Mark Howells-Mead

    THANK YOU so much :)

  • Wta

    Does anyone know of a program or Photoshop plugin that will take a folder of photo image files and the create a single image file with all the images?

 

Related Posts

  • August 13, 2012 -- Image Layout Algorithm – Facebook (1)
    Layout Algorithms: Facebook | Google Plus | Lightbox | Lightbox Android | 500px For the redesign of the Photo Section of Facebook we wanted to highlight some photos by making them bigger. It all started with the following mock by Andy Chung: Layout Alternated Blocks My first shot at ...
  • July 8, 2012 -- Image Layout Algorithm – Lightbox (1)
    Layout Algorithms: Facebook | Google Plus | Lightbox | Lightbox Android | 500px Lightbox.com has a really interesting image layout algorithm. We're going to see how it works and its best use case. How does it work? Column based The algorithm is column based. You pick a number of co...
  • July 7, 2012 -- CSS – Understanding Percentage Background Position (5)
    In this article, I'm going to guide you through a concrete problem I had to solve. Eventually, we'll see how to use percentage values in the background-position CSS property and how it solves a lot of tough issues. Usual way Positioning the image The usual way to position images inside a co...
  • September 22, 2011 -- URLON: URL Object Notation (42)
    #json, #urlon, #rison { width: 100%; font-size: 12px; padding: 5px; height: 18px; color: #560061; } I am in the process of rewriting MMO-Champion Tables and I want a generic way to manage the hash part of the URL (#table__search_results_item=4%3A-slot). I no longer want to write...
  • August 14, 2012 -- Image Layout Algorithm – Facebook – Reordering (2)
    In this article, we are going to see how to support dynamic updates to Facebook Image Layout Algorithm. Beware, this is not an easy task and there are many special cases to handle :) Making images bigger To make images bigger we just run the algorithm all over again with the new image being big....