{"id":27969,"date":"2022-06-27T06:47:44","date_gmt":"2022-06-27T06:47:44","guid":{"rendered":"http:\/\/generate-image-avatars-with-initials-in-javascript"},"modified":"2025-02-09T13:20:31","modified_gmt":"2025-02-09T21:20:31","slug":"generate-image-avatars-with-initials-in-javascript","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/","title":{"rendered":"Generate Image Avatars with Initials in JavaScript"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><p>Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered.\nIn this article, we are going to explore how we can generate avatars based on user initials.<\/p>\n<h2>Codesandbox<\/h2>\n<p>The final version of this project can be viewed on   <a href=\"https:\/\/codesandbox.io\/s\/image-avatars-8yopv5\">Codesandbox<\/a>.<\/p>\n<\/div>\n  \n  <div class=\"wp-block-cloudinary-code-sandbox \">\n    <iframe\n      src=\"https:\/\/codesandbox.io\/embed\/image-avatars-8yopv5?theme=dark&amp;codemirror=1&amp;highlights=&amp;editorsize=50&amp;fontsize=14&amp;expanddevtools=0&amp;hidedevtools=0&amp;eslint=0&amp;forcerefresh=0&amp;hidenavigation=0&amp;initialpath=%2F&amp;module=&amp;moduleview=0&amp;previewwindow=&amp;view=&amp;runonclick=1\"\n      height=\"500\"\n      style=\"width: 100%;\"\n      title=\"image-avatars\"\n      loading=\"lazy\"\n      allow=\"accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking\"\n      sandbox=\"allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts\"\n    ><\/iframe>\n  <\/div>\n\n  <div class=\"wp-block-cloudinary-markdown \"><h2>Github<\/h2>\n<p>Check out the complete source code in this <a href=\"https:\/\/github.com\/musebe\/Avatars-JavaScript\">GitHub Repository<\/a>.<\/p>\n<h2>Pre-requisites<\/h2>\n<p>To follow along through this article you are required to have:<\/p>\n<ul>\n<li>\n<p>Basic knowledge of HTML<\/p>\n<\/li>\n<li>\n<p>Knowledge of Javascript<\/p>\n<\/li>\n<li>\n<p>Some knowledge of Bootstrap<\/p>\n<\/li>\n<\/ul>\n<h2>Introduction<\/h2>\n<p>In this article, we are going to use HTML5, Bootstrap UI framework, and JavaScript to create an application that allows users to generate avatars using their names and the colors of their choice.<\/p>\n<h2>Sample Project Setup<\/h2>\n<p>In this article, I will need you to clone the project from GitHub<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">\ngit <span class=\"hljs-keyword\">clone<\/span> https:<span class=\"hljs-comment\">\/\/github.com\/musebe\/Avatars-JavaScript<\/span>\n\ncd Avatars-JavaScript\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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<p>In your favorite text editor, open the <code>Avatars-JavaScript<\/code> folder, right-click <code>index.html<\/code>, and open the file in your browser. You should be able to see this page after you enter your name, choose your favorite colors, and click generate.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/bowenivan\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/v1655558650\/img_1_e0ymjz.png\" alt=\"img_1.png\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1318\" height=\"460\"\/><\/p>\n<h2>Usage<\/h2>\n<p>Let us now examine the <code>index.js<\/code> to understand the functionality behind this application.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>  <span class=\"hljs-title\">generateAvatar<\/span>(<span class=\"hljs-params\">\n\ntext,\n\nforegroundColor  =  <span class=\"hljs-string\">\"white\"<\/span>,\n\nbackgroundColor  =  <span class=\"hljs-string\">\"black\"<\/span>\n\n<\/span>) <\/span>{\n\n<span class=\"hljs-keyword\">const<\/span>  canvas  =  <span class=\"hljs-built_in\">document<\/span>.createElement(<span class=\"hljs-string\">\"canvas\"<\/span>);\n\ndownload  =  <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"download\"<\/span>);\n\n<span class=\"hljs-keyword\">const<\/span>  context  =  canvas.getContext(<span class=\"hljs-string\">\"2d\"<\/span>);\n\n  \n\ncanvas.width  =  <span class=\"hljs-number\">200<\/span>;\n\ncanvas.height  =  <span class=\"hljs-number\">200<\/span>;\n\n  \n\n<span class=\"hljs-comment\">\/\/ Draw background<\/span>\n\ncontext.fillStyle  =  backgroundColor;\n\ncontext.fillRect(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, canvas.width, canvas.height);\n\n  \n\n<span class=\"hljs-comment\">\/\/ Draw text<\/span>\n\ncontext.font  =  <span class=\"hljs-string\">\"bold 100px Assistant\"<\/span>;\n\ncontext.fillStyle  =  foregroundColor;\n\ncontext.textAlign  =  <span class=\"hljs-string\">\"center\"<\/span>;\n\ncontext.textBaseline  =  <span class=\"hljs-string\">\"middle\"<\/span>;\n\ncontext.fillText(text, canvas.width  \/  <span class=\"hljs-number\">2<\/span>, canvas.height  \/  <span class=\"hljs-number\">2<\/span>  );\n\ndownload.href=canvas.toDataURL(<span class=\"hljs-string\">\"image\/png\"<\/span>);\n\n<span class=\"hljs-keyword\">return<\/span>  canvas.toDataURL(<span class=\"hljs-string\">\"image\/png\"<\/span>); \n\n}\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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<h3>Create the Canvas Element<\/h3>\n<p>The <code>generateAvatar()<\/code> function takes three parameters that is the <code>text<\/code>, <code>foregroundColor<\/code> and <code>backgroundColor<\/code>. We are going to use createElement method to create a canvas element in our HTML document.<\/p>\n<blockquote>\n<p>createElement() method creates an element specified by tagName<\/p>\n<\/blockquote>\n<blockquote>\n<p>HTML canvas is an element that can be used to draw graphics on a web page via scripting (usually JavaScript)<\/p>\n<\/blockquote>\n<pre class=\"js-syntax-highlighted\" 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-keyword\">const<\/span>  canvas  =  <span class=\"hljs-built_in\">document<\/span>.createElement(<span class=\"hljs-string\">\"canvas\"<\/span>);\n\n<\/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<h3>Draw the background<\/h3>\n<p>To get a drawing context on the canvas we use the <code>HTMLCanvasElement.getContext() <\/code>method and takes a contextType parameter <code>getContext(contextType)<\/code> for our case <code>&quot;2d&quot;<\/code> which leads to the creation of an object representing a two-dimensional rendering context.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\n<span class=\"hljs-keyword\">const<\/span>  context  =  canvas.getContext(<span class=\"hljs-string\">\"2d\"<\/span>);\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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<p>We can also specify the height pixels and width pixels of our canvas element by using the height and width attribute of the canvas element created.<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">\ncanvas.width  =  200;\n\ncanvas.height  =  200;\n\n<\/code><\/span><\/pre>\n<p>We can then draw our background by first setting the background color by using the <code>fillStyle<\/code> property and then drawing a filled rectangle with the background set.<\/p>\n<blockquote>\n<p>The fillStyle canvas property sets the color used in filling the drawing.<\/p>\n<\/blockquote>\n<blockquote>\n<\/blockquote>\n<blockquote>\n<p>The fillRect() method draws a filled rectangle. The default background color is black.<\/p>\n<\/blockquote>\n<p>The <code>fillRect()<\/code> takes four parameters, the x-coordinate of the upper corner of the rectangle, the y-coordinate of the upper corner of the rectangle, the width of the rectangle in pixels, and the height of the rectangle in pixels.<\/p>\n<pre class=\"js-syntax-highlighted\" 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\">\/\/ this is the syntax: context.fillRect(x, y, width, height);<\/span>\n\n<span class=\"hljs-comment\">\/\/ in our code context.fillrect(0, 0, 200, 200)<\/span>\n\ncontext.fillRect(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, canvas.width, canvas.height);\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<h3>Draw the text<\/h3>\n<p>To draw the text inside our canvas element we have to set the font using the <code>font<\/code> property, fill the text with color using the <code>fillStyle<\/code> property, text-align our text using the <code>textAlign<\/code> property, set the baseline used when drawing the text using <code>text baseline<\/code> property and draw our text on the canvas using <code>fillText()<\/code> method.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\ncontext.font  =  <span class=\"hljs-string\">\"bold 100px Assistant\"<\/span>;\n\ncontext.fillStyle  =  foregroundColor;\n\ncontext.textAlign  =  <span class=\"hljs-string\">\"center\"<\/span>;\n\ncontext.textBaseline  =  <span class=\"hljs-string\">\"middle\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ context.fillText( text, x, y);<\/span>\n\ncontext.fillText(text, canvas.width  \/  <span class=\"hljs-number\">2<\/span>, canvas.height  \/  <span class=\"hljs-number\">2<\/span>  );  <span class=\"hljs-comment\">\/\/ (text, 100, 100)<\/span>\n\n<\/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<p>We can then return a data URL containing the representation of the image in the format specified by the type parameter in our case `image\/png. To enable the user to download the avatar image we specify the target as the data URL of the image.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\ndownload.href=canvas.toDataURL(<span class=\"hljs-string\">\"image\/png\"<\/span>);\n\n<span class=\"hljs-keyword\">return<\/span>  canvas.toDataURL(<span class=\"hljs-string\">\"image\/png\"<\/span>);\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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<h3>Get inputs from the user<\/h3>\n<p>We can get the values for full name, background color, and text color from the user. so we can use them as arguments to the <code>generateAvatar()<\/code> method. The generate() method wil do this for us and will be called once the <code>generate<\/code> button is clicked.<\/p>\n<pre class=\"js-syntax-highlighted\" 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-keyword\">var<\/span>  name=  <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"name\"<\/span>).value;\n\n<span class=\"hljs-keyword\">var<\/span>  bcgColor=  <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"bcg-color\"<\/span>).value;\n\n<span class=\"hljs-keyword\">var<\/span>  textColor=  <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"text-color\"<\/span>).value;\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<p>After getting the name of our user we can get the initials by first splitting the string using <code>split(&quot; &quot;)<\/code> then we get the first character from the first element and the last element. We then ensure that our initials are in uppercase.<\/p>\n<pre class=\"js-syntax-highlighted\" 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-keyword\">const<\/span>  myNames  =  name.split(<span class=\"hljs-string\">\"  \"<\/span>);\n\n<span class=\"hljs-keyword\">const<\/span>  initials  =  myNames.shift().charAt(<span class=\"hljs-number\">0<\/span>)  +  myNames.pop().charAt(<span class=\"hljs-number\">0<\/span>);\n\n<span class=\"hljs-keyword\">const<\/span>  nameInitials  =initials.toUpperCase();\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<p>Since we defined our avatar div to be of style <code>display: none<\/code> we can now show it and call the generated avatar method passing the values from the user.<\/p>\n<pre class=\"js-syntax-highlighted\" 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-keyword\">var<\/span>  avatarDiv  =  <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"avatarDiv\"<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span>  (avatarDiv.style.display  ===  <span class=\"hljs-string\">\"none\"<\/span>){\n\navatarDiv.style.display  =  <span class=\"hljs-string\">\"block\"<\/span>;\n\n}\n\n<span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"avatar\"<\/span>).src  =  generateAvatar(\n\nnameInitials,\n\ntextColor,\n\nbcgColor\n\n);\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<h2>Conclusion<\/h2>\n<p>In this article, we have learned how we can generate avatars based on the user\u2019s name. Its usefulness is the ability to automatically generate avatars even when the user has not uploaded a profile image.<\/p>\n<p>This can be implemented in websites or apps that have the profile of a user once he\/she has logged in.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":27970,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[134,370,177,371],"class_list":["post-27969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-guest-post","tag-image","tag-javascript","tag-under-review"],"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>Generate Image Avatars with Initials in JavaScript<\/title>\n<meta name=\"description\" content=\"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.\" \/>\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\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generate Image Avatars with Initials in JavaScript\" \/>\n<meta property=\"og:description\" content=\"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-27T06:47:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-09T21:20:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b-jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"728\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Generate Image Avatars with Initials in JavaScript\",\"datePublished\":\"2022-06-27T06:47:44+00:00\",\"dateModified\":\"2025-02-09T21:20:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\"},\"wordCount\":7,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA\",\"keywords\":[\"Guest Post\",\"Image\",\"Javascript\",\"Under Review\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2022\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\",\"name\":\"Generate Image Avatars with Initials in JavaScript\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA\",\"datePublished\":\"2022-06-27T06:47:44+00:00\",\"dateModified\":\"2025-02-09T21:20:31+00:00\",\"description\":\"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA\",\"width\":728,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Generate Image Avatars with Initials in JavaScript\"}]},{\"@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\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Generate Image Avatars with Initials in JavaScript","description":"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.","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\/guest_post\/generate-image-avatars-with-initials-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Generate Image Avatars with Initials in JavaScript","og_description":"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.","og_url":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/","og_site_name":"Cloudinary Blog","article_published_time":"2022-06-27T06:47:44+00:00","article_modified_time":"2025-02-09T21:20:31+00:00","og_image":[{"width":728,"height":400,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b-jpg?_i=AA","type":"image\/jpeg"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/"},"author":{"name":"","@id":""},"headline":"Generate Image Avatars with Initials in JavaScript","datePublished":"2022-06-27T06:47:44+00:00","dateModified":"2025-02-09T21:20:31+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/"},"wordCount":7,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA","keywords":["Guest Post","Image","Javascript","Under Review"],"inLanguage":"en-US","copyrightYear":"2022","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/","url":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/","name":"Generate Image Avatars with Initials in JavaScript","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA","datePublished":"2022-06-27T06:47:44+00:00","dateModified":"2025-02-09T21:20:31+00:00","description":"Avatars create a digital representation of a user and are usually used in web profiles. Most of the time a user is required to upload an image and then used it as the profile. An example of this is Google Mail Services. When a user does not upload a profile image an avatar is generated using his\/her initials. Are you curious about how this magic happens? Don\u2019t worry I\u2019ve got you covered. In this article, we are going to explore how we can generate avatars based on user initials.","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA","width":728,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/generate-image-avatars-with-initials-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Generate Image Avatars with Initials in JavaScript"}]},{"@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":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925788\/Web_Assets\/blog\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b\/23d064d46e9ec3f32a11e1b3101ce1e23954a50c-728x400-1_279708b23b.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/27969","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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=27969"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/27969\/revisions"}],"predecessor-version":[{"id":36780,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/27969\/revisions\/36780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/27970"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=27969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=27969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=27969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}