{"id":16274,"date":"2014-12-07T14:23:38","date_gmt":"2014-12-07T08:53:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16274"},"modified":"2014-12-09T15:32:52","modified_gmt":"2014-12-09T10:02:52","slug":"how-to-get-name-initials-as-an-image","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/","title":{"rendered":"How to get name initials as an image?"},"content":{"rendered":"<p>While making an application, showing user&#8217;s image is a very common use case. Gone were the days when we used to show anonymous image in case a person&#8217;s profile image or display image is not present.<\/p>\n<p>We can easily show a person&#8217;s name initials as an image to identify the person.<\/p>\n<p>Like :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-16279\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47-272x300.png\" alt=\"Screen Shot 2014-12-07 at 13.06.47\" width=\"272\" height=\"300\" srcset=\"https:\/\/www.tothenew.com\/blog\/wp-content\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47-272x300.png 272w, https:\/\/www.tothenew.com\/blog\/wp-content\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47.png 538w\" sizes=\"auto, (max-width: 272px) 100vw, 272px\" \/><\/p>\n<p>Lets consider this html, where we use an angular function defined in the controller, &#8220;$scope.getInitials(name)&#8221; to get an image in the form of a <strong>canvas<\/strong>.<\/p>\n<p><code><br \/>\n&lt;img ng-src=\"{{user.profilePicUrl || getInitials(user.name)}}\" alt=\"{{user.name.first}}\"&gt;<\/code><\/p>\n<p>The function <strong>&#8220;getInitials&#8221;<\/strong>:<\/p>\n<p>[js]<br \/>\n$scope.getInitials = function (name) {<\/p>\n<p>                var canvas = document.createElement(&#8216;canvas&#8217;);<br \/>\n                canvas.style.display = &#8216;none&#8217;;<br \/>\n                canvas.width = &#8217;32&#8217;;<br \/>\n                canvas.height = &#8217;32&#8217;;<br \/>\n                document.body.appendChild(canvas);<br \/>\n                var context = canvas.getContext(&#8216;2d&#8217;);<br \/>\n                context.fillStyle = &quot;#999&quot;;<br \/>\n                context.fillRect(0, 0, canvas.width, canvas.height);<br \/>\n                context.font = &quot;16px Arial&quot;;<br \/>\n                context.fillStyle = &quot;#ccc&quot;;<br \/>\n                var first, last;<br \/>\n                if (name &amp;&amp; name.first &amp;&amp; name.first != &#8221;) {<br \/>\n                    first = name.first[0];<br \/>\n                    last = name.last &amp;&amp; name.last != &#8221; ? name.last[0] : null;<br \/>\n                    if (last) {<br \/>\n                        var initials = first + last;<br \/>\n                        context.fillText(initials.toUpperCase(), 3, 23);<br \/>\n                    } else {<br \/>\n                        var initials = first;<br \/>\n                        context.fillText(initials.toUpperCase(), 10, 23);<br \/>\n                    }<br \/>\n                    var data = canvas.toDataURL();<br \/>\n                    document.body.removeChild(canvas);<br \/>\n                    return data;<br \/>\n                } else {<br \/>\n                    return false;<br \/>\n                }<br \/>\n        }<br \/>\n[\/js]<\/p>\n<p>In above function, we dynamically create a canvas and insert it in the document body using <strong>&#8220;appendChild()&#8221;<\/strong> method.<\/p>\n<p>Then, with <strong>&#8220;getContext()&#8221;<\/strong> function we get an object that provides methods and properties for drawing on that canvas. You can have a look at the canvas methods in details <a href=\"http:\/\/www.w3schools.com\/tags\/ref_canvas.asp\">here<\/a><\/p>\n<p>So, in this way we can get an image using canvas.<\/p>\n<p>Hope this will help \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While making an application, showing user&#8217;s image is a very common use case. Gone were the days when we used to show anonymous image in case a person&#8217;s profile image or display image is not present.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":36,"footnotes":""},"categories":[1],"tags":[1186,377,4842,55,1564],"class_list":["post-16274","post","type-post","status-publish","format-standard","hentry","category-technology","tag-angular-js","tag-canvas","tag-html","tag-javascript","tag-ux"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"While making an application, showing user&#039;s image is a very common use case. Gone were the days when we used to show anonymous image in case a person&#039;s profile image or display image is not present.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"How to get name initials as an image? | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"While making an application, showing user&#039;s image is a very common use case. Gone were the days when we used to show anonymous image in case a person&#039;s profile image or display image is not present.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to get name initials as an image? | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"While making an application, showing user&#039;s image is a very common use case. Gone were the days when we used to show anonymous image in case a person&#039;s profile image or display image is not present.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#article\",\"name\":\"How to get name initials as an image? | TO THE NEW Blog\",\"headline\":\"How to get name initials as an image?\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"\\\/blog\\\/wp-ttn-blog\\\/uploads\\\/2014\\\/12\\\/Screen-Shot-2014-12-07-at-13.06.47-272x300.png\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#articleImage\"},\"datePublished\":\"2014-12-07T14:23:38+05:30\",\"dateModified\":\"2014-12-09T15:32:52+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#webpage\"},\"articleSection\":\"Technology, Angular.js, canvas, HTML\\\/UI\\\/CSS, javascript, UX\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#listItem\",\"name\":\"How to get name initials as an image?\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#listItem\",\"position\":3,\"name\":\"How to get name initials as an image?\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/\",\"name\":\"How to get name initials as an image? | TO THE NEW Blog\",\"description\":\"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-get-name-initials-as-an-image\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2014-12-07T14:23:38+05:30\",\"dateModified\":\"2014-12-09T15:32:52+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to get name initials as an image? | TO THE NEW Blog","description":"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.","canonical_url":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#article","name":"How to get name initials as an image? | TO THE NEW Blog","headline":"How to get name initials as an image?","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47-272x300.png","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#articleImage"},"datePublished":"2014-12-07T14:23:38+05:30","dateModified":"2014-12-09T15:32:52+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#webpage"},"articleSection":"Technology, Angular.js, canvas, HTML\/UI\/CSS, javascript, UX"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/www.tothenew.com\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#listItem","name":"How to get name initials as an image?"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#listItem","position":3,"name":"How to get name initials as an image?","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/sakshi\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/","name":"How to get name initials as an image? | TO THE NEW Blog","description":"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sakshi\/#author"},"datePublished":"2014-12-07T14:23:38+05:30","dateModified":"2014-12-09T15:32:52+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"How to get name initials as an image? | TO THE NEW Blog","og:description":"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.","og:url":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"How to get name initials as an image? | TO THE NEW Blog","twitter:description":"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"16274","title":"How to get name initials as an image? | #site_title","description":"While making an application, showing user's image is a very common use case. Gone were the days when we used to show anonymous image in case a person's profile image or display image is not present.","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 12:29:27","updated":"2024-02-29 08:38:30","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to get name initials as an image?\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Technology","link":"https:\/\/www.tothenew.com\/blog\/category\/technology\/"},{"label":"How to get name initials as an image?","link":"https:\/\/www.tothenew.com\/blog\/how-to-get-name-initials-as-an-image\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16274"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16274\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}