{"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 decoding=\"async\" loading=\"lazy\" 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=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47-272x300.png 272w, \/blog\/wp-ttn-blog\/uploads\/2014\/12\/Screen-Shot-2014-12-07-at-13.06.47.png 538w\" sizes=\"(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":32},"categories":[1],"tags":[1186,377,4842,55,1564],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16274"}],"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}]}}