This is super simple, and requires no code!
Follow these steps and you'll have a masonry style gallery in a few minutes.
1. Add a gallery page in the 'Not Linked' section of your site. Add your images to the gallery.
2. On the page you want to add a Masonry Gallery, insert a Summary Block.
3. Connect it to your Gallery page by selecting correct Gallery. Here are the settings we have used on our Masonry Gallery. You can change the gutter widths and number of items displayed.
4. Boom! Your Masonry Gallery is good to go!