{"id":35147,"date":"2024-08-12T07:00:00","date_gmt":"2024-08-12T14:00:00","guid":{"rendered":"https:\/\/cloudinary.com\/blog\/?p=35147"},"modified":"2026-03-15T13:14:05","modified_gmt":"2026-03-15T20:14:05","slug":"auto-generate-user-avatar-images-ai-next-js","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js","title":{"rendered":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js"},"content":{"rendered":"\n<p>AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a text description. This empowers developers to go beyond traditional avatar solutions, offering users a richer, more interactive experience.&nbsp;<\/p>\n\n\n\n<p>This blog post guides you through building a Next.js application that utilizes Dall-E 3 and Cloudinary to create and store avatars.<\/p>\n\n\n\n<p><a target=\"_blank\" href=\"http:\/\/www.cloudinary.com\" rel=\"noreferrer noopener\">Cloudinary<\/a> securely stores your creations, optimizing them for fast loading and various devices through its global content delivery network (CDN) \u2014 all in one streamlined workflow.<\/p>\n\n\n\n<p>The complete source code of this project is on <a href=\"https:\/\/github.com\/ugwutotheeshoes\/avatar-image-cloudinary-nextjs\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a> and here\u2019s what it looks like.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"511\" data-public-id=\"Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/w_1024,h_511,c_scale\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA\" alt=\"Display the User\u2019s Name, Image, Image URL\" class=\"wp-post-35147 wp-image-35148\" data-format=\"png\" data-transformations=\"f_auto,q_auto\" data-version=\"1723240605\" data-seo=\"1\" srcset=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA 1600w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA 300w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA 768w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA 1024w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240605\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-1.png?_i=AA 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How Dall-E 3 Works<\/h2>\n\n\n\n<p>Dall-E 3 by OpenAI is an <a target=\"_blank\" href=\"https:\/\/platform.openai.com\/docs\/guides\/images\" rel=\"noreferrer noopener\">AI image generation<\/a> model that creates realistic images based on text descriptions. The text is converted into a numerical code; then, it\u2019s fed into a powerful image generation model, which utilizes its vast training in text-image pairs to understand the connection between words and visuals.&nbsp;<\/p>\n\n\n\n<p>The model produces a clear and detailed image, ensuring it aligns with the user&#8217;s description. Remarkably, DALL-E 3 can generate multiple variations of an image with a single prompt, offering users a range of creative options.<\/p>\n\n\n\n<p>Currently, Dall-E 3 isn\u2019t part of the OpenAI API for free public use, as access is currently limited to ChatGPT Plus, Team, and Enterprise users<strong>.&nbsp;<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To get the most out of this article, you must have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A basic understanding of TailwindCSS, Next.js, and Typescript.<\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">A <\/a><a href=\"https:\/\/cloudinary.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">free <\/a><a href=\"https:\/\/cloudinary.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Cloudinary account<\/a>.<\/li>\n\n\n\n<li><a href=\"https:\/\/chatgpt.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">A ChatGPT Plus <\/a><a href=\"https:\/\/chatgpt.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">a<\/a><a href=\"https:\/\/chatgpt.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">ccount<\/a> to access Dall-E 3.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Set Up the Project<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Initialize the Project<\/strong><\/h3>\n\n\n\n<p>With your Cloudinary and ChatGPT Plus account ready, bootstrap a Next.js app with the command below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">npx<\/span> <span class=\"hljs-selector-tag\">create-next-app<\/span><span class=\"hljs-keyword\">@latest<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you receive prompts to set up the app, make sure to select <strong>Typescript<\/strong>, <strong>Tailwind CSS<\/strong>, and <strong>App Router<\/strong>, as they\u2019re essential for building this project. After making your selections, install the necessary dependencies using the following command.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">npm<\/span> <span class=\"hljs-selector-tag\">install<\/span> <span class=\"hljs-selector-tag\">cloudinary<\/span> <span class=\"hljs-selector-tag\">react-copy-to-clipboard<\/span> <span class=\"hljs-keyword\">@types<\/span>\/react-copy-to-clipboard react-icons openai<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>These installed packages perform the following tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cloudinary<\/code>. Provides an interface to interact with Cloudinary.<\/li>\n\n\n\n<li><code>react-copy-to-clipboard<\/code>. Provides a React component that enables users to copy texts with a single click.<\/li>\n\n\n\n<li><code>@types\/react-copy-to=clipboard<\/code>. This is a type definition file for the react-copy-to-clipboard package.<\/li>\n\n\n\n<li><code>react-icons<\/code>. Offers a collection of icons (e.g., Font Awesome or Material UI) that can be easily integrated into your application.<\/li>\n\n\n\n<li><code>openai<\/code>. Interacts with the OpenAI API.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Configure the OpenAI API<\/strong><\/h3>\n\n\n\n<p>Once done, head to the <code>next.config.js<\/code> file in the project&#8217;s root directory and add this code snippet to allow Next.js to display images generated from Cloudinary:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/** <span class=\"hljs-doctag\">@type <span class=\"hljs-type\">{import('next').NextConfig}<\/span> <\/span>*\/<\/span>\n<span class=\"hljs-keyword\">const<\/span> nextConfig = {\n  <span class=\"hljs-attr\">images<\/span>: {\n    <span class=\"hljs-attr\">formats<\/span>: &#91;<span class=\"hljs-string\">\"image\/avif\"<\/span>, <span class=\"hljs-string\">\"image\/webp\"<\/span>],\n    <span class=\"hljs-attr\">remotePatterns<\/span>: &#91;\n      {\n        <span class=\"hljs-attr\">protocol<\/span>: <span class=\"hljs-string\">\"https\"<\/span>,\n        <span class=\"hljs-attr\">hostname<\/span>: <span class=\"hljs-string\">\"res.cloudinary.com\"<\/span>,\n      },\n    ],\n  },\n};\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> nextConfig;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This prioritizes AVIF and WebP formats for image optimization. It also allows Next.js to display images served from the endpoint that Cloudinary&#8217;s API uses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Get the Cloudinary Account and OpenAI API Credentials<\/strong><\/h3>\n\n\n\n<p>Head to <a href=\"http:\/\/www.cloudinary.com\" target=\"_blank\" rel=\"noreferrer noopener\">Cloudinary<\/a> and access the dashboard to find all the required credentials for this project. Copy and store them somewhere safe; you\u2019ll need them for this project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"302\" data-public-id=\"Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/w_1024,h_302,c_scale\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA\" alt=\"Cloudinary Dashboard\" class=\"wp-post-35147 wp-image-35149\" data-format=\"png\" data-transformations=\"f_auto,q_auto\" data-version=\"1723240602\" data-seo=\"1\" srcset=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA 1600w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA 300w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA 768w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA 1024w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240602\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-2.png?_i=AA 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Next, navigate to the <a href=\"https:\/\/beta.openai.com\/account\/api-keys\" target=\"_blank\" rel=\"noreferrer noopener\">OpenAI API key page<\/a> and create a new key, as shown below. The key allows you to interact with Dall-E 3. Copy and store them somewhere safe too.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"511\" data-public-id=\"Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/w_1024,h_511,c_scale\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA\" alt=\"Create an API Key\" class=\"wp-post-35147 wp-image-35150\" data-format=\"png\" data-transformations=\"f_auto,q_auto\" data-version=\"1723240597\" data-seo=\"1\" srcset=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA 1600w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA 300w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA 768w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA 1024w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240597\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Next.js-3.png?_i=AA 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once you have your OpenAI API key and Cloudinary credentials, create a file named <code>.env.local<\/code> in your project&#8217;s root directory and store them.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">\n\/\/ .env.local\nNEXT_PUBLIC_OPENAI_API_KEY=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">openai_api_key<\/span>&gt;<\/span>\nNEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">cloudinary_api_key<\/span>&gt;<\/span>\nNEXT_PUBLIC_CLOUDINARY_API_KEY=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">cloudinary_api_secret<\/span>&gt;<\/span>\nCLOUDINARY_API_SECRET=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">cloudinary_cloud_name<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class='c-callout  c-callout--inline-title c-callout--note'><strong class='c-callout__title'>Note:<\/strong> <p>Never share your credentials publicly.<\/p>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Build the App Component<\/h2>\n\n\n\n<p>For the sake of this post, we limit the app\u2019s functionality to the user avatar generation system, which you can eventually apply or embed in your application.<\/p>\n\n\n\n<p>The App component handles user input (prompts and names) and displays the generated avatar with a user\u2019s name. Navigate to the <code>src\/app\/pages<\/code> file and update it to the code block below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/ src\/app\/pages<\/span>\n<span class=\"hljs-keyword\">import<\/span> { FaRegPaperPlane } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-icons\/fa\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Home<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{  \n  <span class=\"hljs-keyword\">const<\/span> &#91;value, setValue] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> &#91;firstName, setFirstName] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> &#91;lastName, setLastName] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n<span class=\"hljs-keyword\">return<\/span> (    \n<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex min-h-screen flex-col items-center justify-between p-24\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"relative text-3xl font-semibold capitalize \"<\/span>&gt;<\/span>\n        Avatar image manager\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"grid grid-cols-2 gap-2\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"First Name\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"value\"<\/span> <span class=\"hljs-attr\">onChange<\/span>=<span class=\"hljs-string\">{(e)<\/span> =&gt;<\/span> { setFirstName(e.target.value) }} className=\"bg-gray-100 placeholder:text-gray-400 disabled:cursor-not-allowed border border-gray-500 text-gray-900 text-sm rounded-lg block p-3.5 mr-2 w-&#91;290px]\" required \/&gt;\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"Last Name\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"value\"<\/span> <span class=\"hljs-attr\">onChange<\/span>=<span class=\"hljs-string\">{(e)<\/span> =&gt;<\/span> { setLastName(e.target.value) }} className=\"bg-gray-100 placeholder:text-gray-400 disabled:cursor-not-allowed border border-gray-500 text-gray-900 text-sm rounded-lg block p-3.5 mr-2 w-&#91;290px]\" required \/&gt;\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex items-center justify-between\"<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"Enter an image prompt\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"value\"<\/span> <span class=\"hljs-attr\">onChange<\/span>=<span class=\"hljs-string\">{(e)<\/span> =&gt;<\/span> { setValue(e.target.value) }} className=\"bg-gray-100 placeholder:text-gray-400 disabled:cursor-not-allowed border border-gray-500 text-gray-900 text-sm rounded-lg block p-3.5 mr-2 w-&#91;600px]\" required \/&gt;\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text-white bg-blue-700 font-medium rounded-lg text-sm transition-all sm:w-auto px-5 py-3 text-center\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{handlePrompt}<\/span>&gt;<\/span>\n          Send\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span><\/span>\n  )\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The component renders user interface elements for name and text prompts. It uses input fields to capture user input and a button to submit these values to the OpenAI API. Additionally, the component manages the state variables value, firstName, and lastName to store the current user input values. It should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"511\" data-public-id=\"Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/w_1024,h_511,c_scale\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA\" alt=\"App Component\" class=\"wp-post-35147 wp-image-35151\" data-format=\"png\" data-transformations=\"f_auto,q_auto\" data-version=\"1723240593\" data-seo=\"1\" srcset=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA 1600w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA 300w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA 768w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA 1024w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240593\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-4.png?_i=AA 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To provide users with visual feedback during prompt processing, create a Loader component file, <code>src\/components\/Loader.tsx<\/code>, with the code snippet:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/src\/components\/Loader.tsx<\/span>\n<span class=\"hljs-keyword\">const<\/span> Loader = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex justify-center items-center\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"spinner\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n  );\n}\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Loader;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, navigate to the global.css file and add the Loader component\u2019s CSS style.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/src\/app\/global.css<\/span>\n@keyframes spinner {\n  <span class=\"hljs-number\">0<\/span>% {\n    transform: rotate(<span class=\"hljs-number\">0<\/span>deg);\n  }\n  <span class=\"hljs-number\">100<\/span>% {\n    transform: rotate(<span class=\"hljs-number\">360<\/span>deg);\n  }\n}\n.spinner {\n  border: <span class=\"hljs-number\">4<\/span>px solid <span class=\"hljs-comment\">#f3f3f3;<\/span>\n  border-top-color: <span class=\"hljs-comment\">#3498db; \/* Adjust color as desired *\/<\/span>\n  border-radius: <span class=\"hljs-number\">50<\/span>%;\n  animation: spin <span class=\"hljs-number\">1<\/span>s linear infinite;\n}\n@keyframes spin {\n  <span class=\"hljs-number\">0<\/span>% { transform: rotate(<span class=\"hljs-number\">0<\/span>deg); }\n  <span class=\"hljs-number\">100<\/span>% { transform: rotate(<span class=\"hljs-number\">360<\/span>deg); }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Generate and Transform an Image for the User&#8217;s Avatar&nbsp;<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create an API Route<\/strong><\/h3>\n\n\n\n<p>In this step, you\u2019ll send the user&#8217;s prompt to OpenAI&#8217;s API to generate an image, then enhance it using Cloudinary transformations before storing it. The prompt will be sent as a body parameter to the \/api\/generate endpoint on the server. The response from the endpoint containing the transformed image URL will then be retrieved and processed.&nbsp;<\/p>\n\n\n<div class='c-callout  c-callout--inline-title c-callout--note'><strong class='c-callout__title'>Note:<\/strong> <p>For the next step, you\u2019ll use Next.js\u2019 API routes to handle API requests.<\/p>\n<\/div>\n\n\n<p>First, you\u2019ll create a file to serve as an API route for your POST request. To do this, create a folder in the <code>src<\/code> directory called pages\/api. In the <code>pages\/api<\/code> folder, add a new file named generate.ts. Then add this code sample:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/src\/pages\/api\/generate.ts<\/span>\n<span class=\"hljs-keyword\">import<\/span> type { NextApiRequest, NextApiResponse } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'next'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> OpenAI <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"openai\"<\/span>;\n<span class=\"hljs-keyword\">const<\/span> openai = <span class=\"hljs-keyword\">new<\/span> OpenAI({ <span class=\"hljs-attr\">apiKey<\/span>: process.env.NEXT_PUBLIC_DALL_E_3_API_KEY });\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">handler<\/span>(<span class=\"hljs-params\">req: NextApiRequest, res: NextApiResponse<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">try<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> { prompt } = req.body; <span class=\"hljs-comment\">\/\/ Extract the prompt from the request body<\/span>\n    <span class=\"hljs-comment\">\/\/ Generate image using OpenAI<\/span>\n    <span class=\"hljs-keyword\">const<\/span> generatedImage = <span class=\"hljs-keyword\">await<\/span> openai.images.generate({\n      <span class=\"hljs-attr\">model<\/span>: <span class=\"hljs-string\">\"dall-e-3\"<\/span>,\n      <span class=\"hljs-attr\">prompt<\/span>: prompt,\n      <span class=\"hljs-attr\">n<\/span>: <span class=\"hljs-number\">1<\/span>,\n      <span class=\"hljs-attr\">size<\/span>: <span class=\"hljs-string\">\"1024x1024\"<\/span>,\n    });\n    <span class=\"hljs-comment\">\/\/ Extract image URL<\/span>\n    <span class=\"hljs-keyword\">const<\/span> imageUrl = generatedImage.data&#91;<span class=\"hljs-number\">0<\/span>].url;\n  } <span class=\"hljs-keyword\">catch<\/span> (error: any) {\n    <span class=\"hljs-built_in\">console<\/span>.error(error);\n    res.status(<span class=\"hljs-number\">500<\/span>).json({ <span class=\"hljs-attr\">message<\/span>: error.message });\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s a breakdown of the code snippet above:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Line 1:Shows types imported from Next.js to define the structure of the request and response objects.<\/li>\n\n\n\n<li>Lines 2-3:Creates a new OpenAI client instance to interact with OpenAI API.&nbsp;<\/li>\n\n\n\n<li>Line 7: Extracts the prompt property from the request body.<\/li>\n\n\n\n<li>Lines 9-14: Makes an asynchronous call to the OpenAI.images.generate method, specifying Dall-E 3 as the model, using the extracted prompt, requesting one image, and setting the desired size of the image to 1024&#215;1024 pixels.<\/li>\n\n\n\n<li>Line 16:Extracts the image URL from OpenAI API&#8217;s successful response.<\/li>\n\n\n\n<li>Lines 17-20: Catches errors during API calls or processing.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Add Transformations to the Image<\/strong><\/h3>\n\n\n\n<p>The next step involves applying some transformations to the image, uploading it to Cloudinary for storage, and displaying the transformed version and its upload URL. For this, you\u2019ll update the <code>generate.ts<\/code> file as shown below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/pages\/api\/generate.ts<\/span>\n<span class=\"hljs-keyword\">import<\/span> type { NextApiRequest, NextApiResponse } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'next'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> OpenAI <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"openai\"<\/span>;\n+ <span class=\"hljs-keyword\">import<\/span> { v2 <span class=\"hljs-keyword\">as<\/span> cloudinary } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'cloudinary'<\/span>;\n+ cloudinary.config({\n+  cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,\n+  api_key: process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY,\n+  api_secret: process.env.CLOUDINARY_API_SECRET,\n+ });\n<span class=\"hljs-keyword\">const<\/span> openai = <span class=\"hljs-keyword\">new<\/span> OpenAI({ <span class=\"hljs-attr\">apiKey<\/span>: process.env.NEXT_PUBLIC_DALL_E_3_API_KEY });\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">handler<\/span>(<span class=\"hljs-params\">req: NextApiRequest, res: NextApiResponse<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">try<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> { prompt } = req.body; \n    <span class=\"hljs-keyword\">const<\/span> generatedImage = <span class=\"hljs-keyword\">await<\/span> openai.images.generate({\n      <span class=\"hljs-attr\">model<\/span>: <span class=\"hljs-string\">\"dall-e-3\"<\/span>, <span class=\"hljs-attr\">prompt<\/span>: prompt, <span class=\"hljs-attr\">n<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-attr\">size<\/span>: <span class=\"hljs-string\">\"1024x1024\"<\/span>,\n    });\n    <span class=\"hljs-comment\">\/\/ Extract image URL<\/span>\n    <span class=\"hljs-keyword\">const<\/span> imageUrl = generatedImage.data&#91;<span class=\"hljs-number\">0<\/span>].url;\n    <span class=\"hljs-comment\">\/\/ Generate a unique public ID for Cloudinary<\/span>\n+    <span class=\"hljs-keyword\">const<\/span> timestamp = req.body.prompt + <span class=\"hljs-string\">' '<\/span> + <span class=\"hljs-built_in\">Math<\/span>.floor((<span class=\"hljs-built_in\">Math<\/span>.random() * <span class=\"hljs-number\">100<\/span>) + <span class=\"hljs-number\">1<\/span>);\n+    <span class=\"hljs-keyword\">const<\/span> publicId = timestamp.trim().replace(<span class=\"hljs-regexp\">\/\\s+\/g<\/span>, <span class=\"hljs-string\">'-'<\/span>);\n    <span class=\"hljs-comment\">\/\/ Upload image to Cloudinary with transformations<\/span>\n+    <span class=\"hljs-keyword\">const<\/span> response = <span class=\"hljs-keyword\">await<\/span> cloudinary.uploader.upload(imageUrl, {\n+      transformation: &#91;\n+       { <span class=\"hljs-attr\">width<\/span>: <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-attr\">height<\/span>: <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-attr\">gravity<\/span>: <span class=\"hljs-string\">\"face\"<\/span>, <span class=\"hljs-attr\">radius<\/span>: <span class=\"hljs-string\">\"max\"<\/span>, <span class=\"hljs-attr\">border<\/span>: <span class=\"hljs-string\">\"2px_solid_blue\"<\/span>, <span class=\"hljs-attr\">crop<\/span>: <span class=\"hljs-string\">\"thumb\"<\/span> }\n+     ],\n+      resource_type: <span class=\"hljs-string\">'image'<\/span>,\n+      public_id: <span class=\"hljs-string\">`<span class=\"hljs-subst\">${publicId}<\/span>`<\/span>,\n+    });\n    <span class=\"hljs-comment\">\/\/ Extract uploaded image URL from Cloudinary response<\/span>\n+    <span class=\"hljs-keyword\">const<\/span> uploadedImageUrl = response.secure_url;\n    <span class=\"hljs-comment\">\/\/ Send successful response with uploaded image URL<\/span>\n+    res.status(<span class=\"hljs-number\">200<\/span>).json({ <span class=\"hljs-attr\">image_url<\/span>: uploadedImageUrl });\n  } <span class=\"hljs-keyword\">catch<\/span> (error) {\n    <span class=\"hljs-built_in\">console<\/span>.error(error);\n    res.status(<span class=\"hljs-number\">500<\/span>).json({ <span class=\"hljs-attr\">message<\/span>: error.message });\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code sample above do the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lines 4-9: Imports the Cloudinary Node.js SDK (version 2) and configures it using the environment variables.<\/li>\n\n\n\n<li>Line 20 and 21: Generates a unique identifier for the uploaded image by combining the user&#8217;s prompt with random numbers, removing spaces, and replacing them with hyphens for a valid Cloudinary ID.<\/li>\n\n\n\n<li>Lines 23-29: Handles uploading the image to Cloudinary, specifying it as an image resource, and assigning a unique public ID. It also applies various transformations before storage. Cloudinary significantly enhances the image manipulation process, offering transformations beyond what simple prompts can achieve. Here&#8217;s a breakdown of the transformations:\n<ul class=\"wp-block-list\">\n<li><code>width: 200, height: 200<\/code> resizes the image to 100&#215;100 pixels.<\/li>\n\n\n\n<li><code>gravity: \"face\"<\/code> centers the image on a face, if there\u2019s any detected.<\/li>\n\n\n\n<li><code>radius: \"max\"<\/code> crops the image to the shape of an <a href=\"https:\/\/cloudinary.com\/tools\/ellipse-crop-image\">ellipse<\/a> or a circle.<\/li>\n\n\n\n<li><code>border: \"2px_solid_blue\"<\/code> adds a solid blue border of 2 pixels.<\/li>\n\n\n\n<li><code>crop: \"thumb\"<\/code> creates a thumbnail for the image.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Lines 31 and 33: Extracts the secure URL of the uploaded image from the Cloudinary upload response and sends it as a successful response in a JSON object.<\/li>\n\n\n\n<li>Lines 34-37: Catches any errors that might occur during the upload process.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Send a Prompt to Generate the Image<\/strong><\/h3>\n\n\n\n<p>Next, you\u2019ll need to send data to the API route. Head back to your <code>src\/app\/pages<\/code> file, create a function called handlePrompt, and trigger the function on the button element\u2019s click. Then, convert the value state variable (containing the prompt) into a JSON object and set it as the API request body.&nbsp;<\/p>\n\n\n\n<p>To handle the response from the API route, you\u2019ll store the transformed image URL from the response in a state variable named URL. You\u2019ll also create a function, <code>handleCharacters<\/code>, and a state variable, characters, to extract and store the user&#8217;s name. Do these with the code snippet below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/ src\/app\/pages<\/span>\n<span class=\"hljs-keyword\">import<\/span> { FaRegPaperPlane } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-icons\/fa\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Home<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n<span class=\"hljs-keyword\">const<\/span> &#91;url, setUrl] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;loading, setLoading] = useState&lt;boolean | <span class=\"hljs-literal\">undefined<\/span>&gt;(<span class=\"hljs-literal\">false<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;characters, setCharacters] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);  \n<span class=\"hljs-keyword\">const<\/span> &#91;value, setValue] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;firstName, setFirstName] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;lastName, setLastName] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> handleCharacters = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> char = firstName + <span class=\"hljs-string\">\" \"<\/span> + lastName\n    setCharacters(char)\n  }\n<span class=\"hljs-keyword\">const<\/span> handlePrompt = <span class=\"hljs-keyword\">async<\/span> (e: any) =&gt; {\n    e.preventDefault()\n    setLoading(<span class=\"hljs-literal\">true<\/span>)\n    <span class=\"hljs-keyword\">try<\/span> {\n      <span class=\"hljs-keyword\">const<\/span> response = <span class=\"hljs-keyword\">await<\/span> fetch(<span class=\"hljs-string\">'\/api\/dalle3'<\/span>, {\n        <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">'POST'<\/span>,\n        <span class=\"hljs-attr\">headers<\/span>: { <span class=\"hljs-string\">'Content-Type'<\/span>: <span class=\"hljs-string\">'application\/json'<\/span> },\n        <span class=\"hljs-attr\">body<\/span>: <span class=\"hljs-built_in\">JSON<\/span>.stringify({ <span class=\"hljs-attr\">prompt<\/span>: value }), <span class=\"hljs-comment\">\/\/ send the string as a JSON object<\/span>\n      });\n      <span class=\"hljs-comment\">\/\/ Handle success, such as updating UI or showing a success message<\/span>\n      <span class=\"hljs-keyword\">if<\/span> (response.ok) {\n        <span class=\"hljs-keyword\">const<\/span> data = <span class=\"hljs-keyword\">await<\/span> response.json();\n        setUrl(data.image_url)\n        setValue(<span class=\"hljs-string\">\"\"<\/span>)\n        handleCharacters()\n      }\n    } <span class=\"hljs-keyword\">catch<\/span> (error) {\n      <span class=\"hljs-comment\">\/\/ Handle network errors or other exceptions<\/span>\n      <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error uploading file:'<\/span>, error);\n    }\n  };\n<span class=\"hljs-keyword\">return<\/span> (    \n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex min-h-screen flex-col items-center justify-between p-24\"<\/span>&gt;<\/span>\n    ... \/\/ App Title\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text-white bg-blue-700 font-medium rounded-lg text-sm transition-all sm:w-auto px-5 py-3 text-center\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{handlePrompt}<\/span>&gt;<\/span>\n            Send\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span><\/span>\n  )\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The handleCharacters function takes the firstName and lastName values, combines them into a single string, and updates the characters state variable to reflect the concatenated name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Display the User\u2019s Name, Image, and Image URL<\/h2>\n\n\n\n<p>Next, you\u2019ll display the user\u2019s name, transformed image, and image URL only when the response from the server is successful. To achieve this, render the image and name only when the url state variable has a value.&nbsp;<\/p>\n\n\n\n<p>Then, hide the loading indicator using the <code>onLoadingComplete<\/code> prop, indicating successful image retrieval. This prop will also update the button&#8217;s content based on whether the application has processed the request.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/ src\/app\/pages<\/span>\n<span class=\"hljs-keyword\">import<\/span> Image <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"next\/image\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Home<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n<span class=\"hljs-keyword\">const<\/span> &#91;url, setUrl] = useState&lt;string&gt;(<span class=\"hljs-string\">\"\"<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;loading, setLoading] = useState&lt;boolean | <span class=\"hljs-literal\">undefined<\/span>&gt;(<span class=\"hljs-literal\">false<\/span>);\n<span class=\"hljs-keyword\">return<\/span> (    \n  <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex min-h-screen flex-col items-center justify-between p-24\"<\/span>&gt;<\/span>\n    ... \/\/App Title\n{url &amp;&amp;\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex flex-col items-center justify-center\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"flex justify-center items-center gap-4\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Image<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{url}<\/span> <span class=\"hljs-attr\">onLoadingComplete<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> setLoading(false)} width={192} height={192} alt=\"ai image\" \/&gt;\n            {!loading &amp;&amp; <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text-4xl \"<\/span>&gt;<\/span>{characters}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>}\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n       }       \n      ... \/\/input \n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text-white bg-blue-700 font-medium rounded-lg text-sm transition-all sm:w-auto px-5 py-3 text-center\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{handlePrompt}<\/span>&gt;<\/span>\n            {loading ? <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Loader<\/span> \/&gt;<\/span> : \"Send\"}\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span><\/span>\n  )\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, you\u2019ll display the transformed image URL. To display the image URL:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a ternary operator to render an input field only when the loading state variable is false, including a button to copy the input value.<\/li>\n\n\n\n<li>Bind the input value to the <code>url<\/code> state variable to update the input field with the image URL.<\/li>\n\n\n\n<li>Enclose the button element in a <a href=\"https:\/\/www.npmjs.com\/package\/react-copy-to-clipboard\" target=\"_blank\" rel=\"noreferrer noopener\"><code>CopyToClipboard<\/code><\/a> component. This component allows a user to copy the input value (image URL) to the clipboard. The component takes two props: <code>text<\/code> (string) defines the text to be copied (typically the transformed image URL), and <code>onCopy<\/code> (function) triggers a function called <code>onCopyText<\/code> whenever the copy operation is successful.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>The function <code>onCopyText<\/code> will display a visual confirmation message once a user copies the URL. Here\u2019s what it looks like.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">\n<span class=\"hljs-comment\">\/\/ src\/app\/pages<\/span>\nimport Image from <span class=\"hljs-string\">\"next\/image\"<\/span>;\nimport { useState } from <span class=\"hljs-string\">\"react\"<\/span>;\nexport <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Home<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n<span class=\"hljs-keyword\">const<\/span> &#91;loading, setLoading] = useState&lt;boolean | undefined&gt;(<span class=\"hljs-keyword\">false<\/span>);\n<span class=\"hljs-keyword\">const<\/span> &#91;copyStatus, setCopyStatus] = useState(<span class=\"hljs-keyword\">false<\/span>); \n <span class=\"hljs-comment\">\/\/ saveImage Function <\/span>\n <span class=\"hljs-comment\">\/\/ handlePrompt <\/span>\n  <span class=\"hljs-keyword\">const<\/span> onCopyText = () =&gt; {\n    setCopyStatus(<span class=\"hljs-keyword\">true<\/span>);\n    setTimeout(() =&gt; setCopyStatus(<span class=\"hljs-keyword\">false<\/span>), <span class=\"hljs-number\">2000<\/span>); <span class=\"hljs-comment\">\/\/ Reset status after 2 seconds<\/span>\n  };\n<span class=\"hljs-keyword\">return<\/span> (    \n  &lt;main className=<span class=\"hljs-string\">\"flex min-h-screen flex-col items-center justify-between p-24\"<\/span>&gt;\n    ... <span class=\"hljs-comment\">\/\/App Title<\/span>\n{url &amp;&amp;\n    ... <span class=\"hljs-comment\">\/\/ Image and User Name<\/span>\n           {!loading &amp;&amp;\n            &lt;div className=<span class=\"hljs-string\">\"mt-8\"<\/span>&gt;\n            &lt;div className=<span class=\"hljs-string\">\" flex justify-center items-center gap-2 mt-2\"<\/span>&gt;\n              &lt;p className=<span class=\"hljs-string\">\"text-sm font-semibold\"<\/span>&gt;Image URL:&lt;\/p&gt;\n              &lt;input id=<span class=\"hljs-string\">\"clipboard\"<\/span> value={url} className=<span class=\"hljs-string\">\"bg-gray-100 border border-gray-500 text-gray-900 text-sm rounded-lg p-1.5 w-&#91;340px]\"<\/span> \/&gt;\n              &lt;CopyToClipboard text={url} onCopy={onCopyText}&gt;\n                &lt;button className=<span class=\"hljs-string\">\"text-sm font-semibold text-blue-500\"<\/span>&gt;\n                  &lt;FaRegCopy \/&gt;\n                &lt;\/button&gt;\n              &lt;\/CopyToClipboard&gt;\n            &lt;\/div&gt;\n            {copyStatus &amp;&amp; &lt;p className=<span class=\"hljs-string\">\"text-center\"<\/span>&gt;Text copied to clipboard!&lt;\/p&gt;}\n          &lt;\/div&gt;\n          }\n        &lt;\/div&gt;\n       }       \n      ... <span class=\"hljs-comment\">\/\/Input and Button<\/span>\n  &lt;\/main&gt;\n  )\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The app should look similar to this when you enter a name and a prompt with \u201ca siamese cat\u201d:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"511\" data-public-id=\"Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/w_1024,h_511,c_scale\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA\" alt=\"Display the User\u2019s Name, Image, Image URL\" class=\"wp-post-35147 wp-image-35152\" data-format=\"png\" data-transformations=\"f_auto,q_auto\" data-version=\"1723240590\" data-seo=\"1\" srcset=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA 1600w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA 300w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA 768w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA 1024w, https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723240590\/Web_Assets\/blog\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5\/blog-Auto-Generate-User-Avatar-Images-With-AI-and-Cloudinary-in-Nextjs-5.png?_i=AA 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Test the App<\/h2>\n\n\n\n<p>After setting up the app, users can enter a text prompt describing their desired image, and Dall-E 3 will generate a unique image based on their input. Then, the image undergoes transformations using Cloudinary before being stored for later use. The application will <a href=\"https:\/\/www.loom.com\/share\/64a80cd84cbf46d69ff15d282db765ce?sid=e224fa84-05f3-4d9b-850f-d6276fcac964\">look like this<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Importance of Cloudinary in AI Image Delivery<\/h2>\n\n\n\n<p>While Cloudinary is great at storing AI-generated images, it also ensures they are delivered quickly over a CDN, enhancing your application&#8217;s performance. Other Cloudinary features are important for a smooth user experience, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Automatic <\/strong><strong>o<\/strong><strong>ptimizations<\/strong><strong>.<\/strong> Reduced storage footprint and faster loading times.<\/li>\n\n\n\n<li><strong>On-the-<\/strong><strong>fly t<\/strong><strong>ransformations<\/strong><strong>.<\/strong> Image delivery in various forms and sizes.<\/li>\n<\/ul>\n\n\n\n<p>On-the-fly transformations to uploaded AI-images is a personal favorite.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This blog post demonstrates how to generate AI images through Dall-E 3 and upload them to Cloudinary. This streamlined workflow empowers users to generate, modify, and store AI images in a single platform. Eventually, you\u2019ll embed this function in your application or build a standalone product with it. To learn more about how integrating advanced solutions like Cloudinary\u2019s CDN brings a new level of optimization and customization to content delivery, <a href=\"https:\/\/cloudinary.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">contact us today<\/a>.<\/p>\n\n\n\n<p>If you found this post helpful and would like to learn more, feel free to join the <a target=\"_blank\" href=\"https:\/\/community.cloudinary.com\/\" rel=\"noreferrer noopener\"><u>Cloudinary Community forum<\/u><\/a> and its associated <a target=\"_blank\" href=\"https:\/\/discord.com\/invite\/cloudinary\" rel=\"noreferrer noopener\"><u>Discord<\/u><\/a>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/platform.openai.com\/docs\/guides\/images\" target=\"_blank\" rel=\"noreferrer noopener\">DallE-3 Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/documentation\" target=\"_blank\" rel=\"noreferrer noopener\">Cloudinary Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.npmjs.com\/package\/react-copy-to-clipboard\" target=\"_blank\" rel=\"noreferrer noopener\">react-copy-to-clipboard<\/a> package<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a text description. This empowers developers to go beyond traditional avatar solutions, offering users a richer, more interactive experience.&nbsp; This blog post guides you through building a Next.js application that [&hellip;]<\/p>\n","protected":false},"author":87,"featured_media":35154,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-35147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Auto-Generate User Avatar Images With AI and Cloudinary in Next.js<\/title>\n<meta name=\"description\" content=\"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js\" \/>\n<meta property=\"og:description\" content=\"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-12T14:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T20:14:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"melindapham\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\"},\"author\":{\"name\":\"melindapham\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/0d5ad601e4c3b5be89245dfb14be42d9\"},\"headline\":\"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js\",\"datePublished\":\"2024-08-12T14:00:00+00:00\",\"dateModified\":\"2026-03-15T20:14:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\"},\"wordCount\":1729,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA\",\"inLanguage\":\"en-US\",\"copyrightYear\":\"2024\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\",\"url\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\",\"name\":\"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA\",\"datePublished\":\"2024-08-12T14:00:00+00:00\",\"dateModified\":\"2026-03-15T20:14:05+00:00\",\"description\":\"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA\",\"width\":2000,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/0d5ad601e4c3b5be89245dfb14be42d9\",\"name\":\"melindapham\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e6f989fa97fe94be61596259d8629c3df65aec4c7da5c0000f90d810f313d4f4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e6f989fa97fe94be61596259d8629c3df65aec4c7da5c0000f90d810f313d4f4?s=96&d=mm&r=g\",\"caption\":\"melindapham\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js","description":"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js","og_locale":"en_US","og_type":"article","og_title":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js","og_description":"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a","og_url":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js","og_site_name":"Cloudinary Blog","article_published_time":"2024-08-12T14:00:00+00:00","article_modified_time":"2026-03-15T20:14:05+00:00","og_image":[{"width":2000,"height":1100,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","type":"image\/jpeg"}],"author":"melindapham","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js"},"author":{"name":"melindapham","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/0d5ad601e4c3b5be89245dfb14be42d9"},"headline":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js","datePublished":"2024-08-12T14:00:00+00:00","dateModified":"2026-03-15T20:14:05+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js"},"wordCount":1729,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","inLanguage":"en-US","copyrightYear":"2024","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js","url":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js","name":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","datePublished":"2024-08-12T14:00:00+00:00","dateModified":"2026-03-15T20:14:05+00:00","description":"AI advancements have led to a surge in personalized experiences. Instead of using generic avatars, you can now create a unique digital persona from just a","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","width":2000,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/auto-generate-user-avatar-images-ai-next-js#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Auto-Generate User Avatar Images With AI and Cloudinary in Next.js"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/0d5ad601e4c3b5be89245dfb14be42d9","name":"melindapham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e6f989fa97fe94be61596259d8629c3df65aec4c7da5c0000f90d810f313d4f4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e6f989fa97fe94be61596259d8629c3df65aec4c7da5c0000f90d810f313d4f4?s=96&d=mm&r=g","caption":"melindapham"}}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1723222950\/avatars_with_dalle_cldn-blog\/avatars_with_dalle_cldn-blog.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/35147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=35147"}],"version-history":[{"count":3,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/35147\/revisions"}],"predecessor-version":[{"id":39887,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/35147\/revisions\/39887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/35154"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=35147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=35147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=35147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}