Skip to content

Build a Gravatar Service in Next.js

Part 1 of this article describes how Nextvatar, a Gravatar service, works to enable users upload images and apply transformations to them. We covered the following:

  1. Installation of the Cloudinary React library
  2. Implementation of the Cloudinary Upload Widget
  3. Managing a connection to Cloudinary to retrieve an uploaded image’s image link

This post, part 2 of the series, will highlight the use of Cloudinary React to apply transformations on the parent image element to get our avatar.

You can access the entire project on Codesandbox. Fork it to run the code.

As we mentioned during setup in part 1, these are necessary to have.

  • An account on Cloudinary with a cloud name
  • NodeJS installed on our computer
  • A code editor of our choice

We install lodash in our nextvatar project. Lodash is a modern JavaScript library delivering modularity, performance.

    npm install lodash

In the first part of the post, we configured the Cloudinary Widget to retrieve the delivery URL of the stored image. Now we will do the same for the public ID. The public ID is a unique identifier for each stored assets on Cloudinary.


<!— Please mention to include to import Avatar in pages/index.js [ import Avatar from “../components/Avatar”; ]—>

    import React, { useState } from "react";
    import Avatar from "../components/Avatar”;
    {/* other imports goes here */}
    export default function IndexPage() {
      const [publicId, setPublicId] = useState(null);
      const widget = () => {
              cloudName: "terieyenike",
              uploadPreset: "avatar"
            (error, result) => {
              if (!error && result && result.event === "success") {
      return (
        <div className={styles.container}>
            {/* Rendered JSX goes here */}
              <p className={styles.pStyle}>
                <span>Avatar image:</span>
                <Avatar imageID={publicId} />
Code language: HTML, XML (xml)

In the above snippet, we configured the Cloudinary upload widget using our Cloudinary Cloud name and a specific upload preset.

We returned the public ID from the above code after successfully uploading it to the media store, Cloudinary. We displayed the image as an avatar in the Avatar component with the imageID prop having the returned image’s public ID as its value.

Here, we will work on the Avatar component to apply image transformation and configuration parameters to the image. We will use the CloudinaryContext component shipped with the cloudinary-react library to provide shared configuration across all child Cloudinary components. In the components directory, we create a file called Avatar.js with the following content.


<!— Can you please mention to create ‘Avatar.js’ file within components —>

    import { Image, CloudinaryContext, Transformation } from "cloudinary-react";
    import styles from "../styles/Home.module.css";
    const Avatar = ({ imageID = "hwzsj8ybu62tgk7oohh0" }) => {
      return (
          <section className={styles.section}>
            <div className={styles.container}>
              <CloudinaryContext cloudName="terieyenike">
                <Image publicId={imageID}>
    export default Avatar;
Code language: JavaScript (javascript)

The code above does the following:

  • CloudinaryContext defines the shared parameters applied to all children elements
  • Image element represents the Cloudinary Image tag
  • Transformation allows you to use additional styles, formats, and other visual enhancements to the image
  • imageID, a default public id passed as a parameter in the component till we get the displayed image

The supplied transformation does the following:

  • Applies an art effect of incognito to the image.
  • Uses the ‘gravity’ transformation to automatically detect a face and focus on it
  • Sets a height and width of 150px to the image
  • Applies a dynamic quality and format transformation to match the device and internet bandwidth
  • Creates a circular, thumbnail image with the crop and thumb transformations.

You can find more Cloudinary image transformations here.

With this done, we have the transformed avatar looking like this.

This post discussed how to use Cloudinary and its transformation parameters to create an avatar. Try to apply this system to any image stored in your media library, and modify the image to your specifications.

You may find this helpful:

Back to top

Featured Post