Upload a Custom Font to Cloudinary Using the Node SDK
A tutorial on how to upload and use custom fonts in text overlays with Cloudinary to generate images with custom text.
If you want to use custom fonts with Cloudinary, you need to upload them as authenticated assets. This script will allow you to upload an OTF or TTF to use when adding text overlays to images.
Create a project and install dependencies
Make sure you’ve created a folder, initialized the Node project, and installed the cloudinary
Node SDK:
Upload the custom font to Cloudinary
Create a file in this folder called upload.js
and add the following inside:
Update the information at the top with your own Cloudinary and font info, then run the script:
You will see the output of the script showing the upload details (or an error, if one occurred). It will look something like this (some details masked):
NOTE: this example uses the free Tahu! font.
Use the custom font in a Cloudinary image
Now that we’ve uploaded the font, we can use it for image overlays.
Let’s start with this image of a llama:
The URL to display this image is:
We can break this URL into four pieces:
https://res.cloudinary.com/jlengstorf/image/upload
— this is the URL base, which is going to be the same for all images in my Cloudinary account/w_800,q_auto,f_auto
— these are image transforms tells Cloudinary that I want the image at 800 pixels wide and that the format and quality should be set automatically to best suit this browser/v1578269094
— this is teh version, an optional piece that allows Cloudinary to cache the image aggressively to cut down on bandwidth and processing/llama-kimmy-williams.jpg
— this is the public ID of the image we want to display and an optional extension telling Cloudinary what format should be used (this will be overridden byf_auto
, though)
To insert our custom text overlay, we’ll add a new piece in between the image transforms and the version, which will configure text:
This sets a few things for our text:
g_south
— put the text at the bottom-center of the imagey_20
— move the text 20px away from the bottom of the imageco_white
— set the text color to whitel_text:Tahu!.ttf_70
—l_text
is the overlay config, then we use the custom font’s public ID, then_70
to set the font size:Have%20you%20been%20to%20Machu%20Picchu%3F
— we can set any text we want here — the text needs to be URI-encoded
Once we’ve added this to the URL, it looks like this:
And the image now includes our text overlay using the custom font!
What to do next
For next steps:
- Learn how to use text overlays to automatically generate social sharing cards for blog posts
- Get more detail on how text overlays work
- Use the media library to upload custom fonts via UI
- Read the Cloudinary docs on text overlays