A really useful in-depth tutorial on using flexbox to create a gallery layout like Google Images:
Pure CSS implementation of Google Photos / 500px image layout
Note that part of this technique requires certain inline style declarations to be present, based on the size of the image. From what I can see it’ll be fine to use PHP to do the image calculations and output the styles server-side.
As and when (if?) I implement this somewhere, I’ll post the PHP here, but I’m confident it’ll be very straightforward.