How To Build a Google Photos Clone - Part 1
Want to know how to implement a complex web application like Google Photos? Learn how in this video series.
Join the DZone community and get the full member experience.Join For Free
Ever wanted a sneak peek into how a web application like Google Photos is built from the ground up?
Then let's re-implement (some of) its functionality with Java and, along the way, discuss not only the "how" but also the "why" we are implementing things a certain way.
Part 1: Generating thumbnails for a directory of photos with Java and ImageMagick, which will serve as the basis for our photo backend service.
What’s in the Video
There are many ways to tackle a complex piece of software like Google Photos, but I'd like to start with a throwaway, spike dummy project and focus on generating thumbnails in this video.
(Quick Note: As always, you'll find all the sources I'm working with on GitHub so that you can follow along easily.)
While Java has an Image API, it is rather limited and we need something more advanced. In comes, the ImageMagick library, which can convert more than 200 different image file formats, extract thumbnails and much more - in short, everything we need for this video.
First off, you'll need to install ImageMagick and I'll show you the most painless way to do so, by using a package manager. You can then proceed to clone the sample images project and the project skeleton I am working with, to start converting a couple of test images.
After having opened the dummy project in your IDE, we can finally start coding. I will be using JetBrain's Fleet IDE throughout the video series, but any IDE with basic Maven support will work.
First, we'll need to tackle creating a thumbnail. On the command line, it is relatively easy to call ImageMagick, to dynamically resize images to a certain width and height. Hence, our challenge is to use Java's Process API to spawn external ImageMagick processes and also to make sure they shut down gracefully, i.e. not hang around forever whenever a problem occurs.
Once we can extract thumbnails from single images, up next is extracting thumbnails for whole directories. That means using Java's Path API to walk a whole file tree, including its subdirectories, filtering out the images and then feeding them to multiple, sequential ImageMagick processes. Along the way, we need to figure out how to decide which file is an image and how a corresponding
isImage() method could be implemented.
Let's take a short break and reflect on what we did so far. It is not the best and the most bullet-proof code ever written, but we have something working! And that's what's important for now - having a working version pretty soon, instead of debating endlessly over architectural decisions we don't need to solve right now.
Hence, we're going to take our application and deploy it to a local Linux NAS - as I developed everything on a Windows machine and I want to see stuff work on Linux as well. The problem is: As soon as we execute our app on the NAS, it fails and no images are being generated. Why? The NAS had Java and ImageMagick installed?
You'll find out in the next episode.
Opinions expressed by DZone contributors are their own.