How to create a selfie generator
Last updated
Last updated
In 2023, several major marketing campaigns used the Remove Background API to create selfie generators that allowed users to share personalized branded selfies on their social media.
Among them were the Barbie Movie, Netflix's Black Mirror and Taylor Swift's 1989 album.
In this tutorial, I want to show you how easy it is to build a web app that offers the same set of features, simply by leveraging the Remove Background API and a bit of JavaScript.
If you're curious to see a demo of the web app we're going to build, one is available here: https://photoroom-api-web-demo.vercel.app.
You will need an API key, you can get it here: https://app.photoroom.com/api-dashboard
Important: This tutorial focuses on the key parts of the implementation. If you want to experiment with the code, I recommend you check out the full codebase, which is open-source and available on GitHub.
To allow users to pick the image they want to use, we can use a standard <input>
tag:
Then, we need to upload the file to the Remove Background API.
The implementation is pretty straightforward since the API call is a standard HTTP POST request:
You will need an API key, you can get it here: https://app.photoroom.com/api-dashboard
After we have received the image with the background removed, we need to use it to compose the final image.
The trick to getting to the final image is to place the background-less image between a background and an overlay layer:
The code that implements this process is inside the function drawImageWithOverlay()
:
This function contains a bit of code, but the gist of it is pretty simple:
we create a canvas and draw the background image
then, we scale the user’s image and draw it on top
finally, we draw the overlay image on top of everything else
We’re almost there, we only need one last thing: allowing the user to input some text and overlaying it on the image.
Allowing users to set the text is simple: once again we use a standard <input>
tag:
The more challenging part is to draw the text on the canvas, with the curvature taken into account.
For this, we need to add some code to the function drawImageWithOverlay():
As you can see, this code is quite complex. But here’s the logic behind it:
We iterate over the lines of text
For each line, we compute the coordinates of the semi-circle that we will use as a baseline
Then, we iterate over each word and each character in order to draw each character on that baseline with the correct rotation
We’re almost finished, all that’s left to do is add a button that will allow users to download the final image so that they can share it on their social media.
To do this we’re first going to add a <button>
to the HTML:
And then we will add the code to handle the download:
Here the strategy is quite simple:
we take the content of the canvas
we turn it into a binary file representation
we set the href
of the download tag to the URL of that binary representation
That’s it, thanks to the Remove Background API and a bit of 2D drawing code in JavaScript, we’ve been able to implement a simple web app that enables users to:
upload a selfie
customize it using pre-defined visual assets
overlay some text on top
download the result
From that point, it would be pretty easy to reuse this web app for an actual marketing campaign.
All you would need is to replace the background, overlay, and text font with the actual branded assets you want to use for the campaign.
The full codebase is open-source and available on GitHub.