{"id":223,"date":"2005-03-20T16:12:58","date_gmt":"2005-03-20T15:12:58","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2005\/03\/20\/gravatar-and-the-broken-image-icon\/"},"modified":"2007-05-08T16:15:02","modified_gmt":"2007-05-08T14:15:02","slug":"gravatar-and-the-broken-image-icon","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2005\/03\/gravatar-and-the-broken-image-icon\/","title":{"rendered":"Gravatar and the Broken Image Icon"},"content":{"rendered":"<p>I&#39;m currently redesigning this site and I think I&#39;m going to give <a href=\"http:\/\/www.gravatar.com\/\">Gravatar<\/a> a go. Something I&#39;ve been worrying about is the fact that Gravatar is centralized, so when its site happens to be (momentarily and exceptionnally) offline, displaying a gravatar shows the ugly <em>broken image icon<\/em>.<\/p>\n<p>So, I&#39;ve come up with a simple and CSS way to avoid this. Display a proper avatar when available, display no image when offline.<\/p>\n<p><!--more--><\/p>\n<h2>Wait, wait. Gra-va-what ?<\/h2>\n<p><a href=\"http:\/\/www.gravatar.com\/\">Gravatar<\/a> is a free centralized database of <a href=\"http:\/\/www.google.com\/search?q=define%3Aavatar\">avatars<\/a>, hence its name : <strong>g<\/strong>lobally <strong>r<\/strong>ecognized <strong>avatar<\/strong>. On all the sites and weblogs using Gravatar, the same avatar can impersonnify you. And there are more and more weblogs using Gravatar, including <a href=\"http:\/\/www.gravatar.com\/traffic.php\" title=\"popular sites using Gravatar\">very popular ones<\/a>.<\/p>\n<p>In one word : it&#39;s cool. Can we go on ? :)<\/p>\n<h2>The problem<\/h2>\n<p>Sometimes a website can become unavailable. Trying to display an image from this server only shows a tiny red cross, also known as &quot;<em>the broken image icon<\/em>&quot;. While it can sometimes actually <a href=\"http:\/\/www.errorwear.com\/steph-brokenimage.html\">be pretty<\/a>, most of the times, it sucks. Firefox users have some options to avoid this (in <a href=\"about:config\">about:config<\/a> set <strong>browser.display.show_image_placeholders<\/strong> to <strong>false<\/strong>) but as far as I know, there no such thing in MSIE.<\/p>\n<h2>The solution<\/h2>\n<p>In short, instead of using an IMG tag, the Gravatar URL is specified in a CSS style sheet. The PHP code generating the avatar is mostly similar to the <a href=\"http:\/\/www.gravatar.com\/implement.php#section_3_1\">original code<\/a>. The paths and vars used here are &quot;generic&quot;, you&#39;ll have to figure out how to adapt this for your blog on your own.<\/p>\n<p>You&#39;ll need two images :<\/p>\n<ol>\n<li>a default avatar, used when the commenters did not register with Gravatar, named gravatar_default.gif here<\/li>\n<li>a transparent image, like a single transparent pixel, named pixel.gif here<\/li>\n<\/ol>\n<p>The uber lazy bloggers can steal mine : <a href=\"\/blog\/images\/gravatar_default.gif\">gravatar_default.gif<\/a> and <a href=\"\/blog\/images\/pixel.gif\">pixel.gif<\/a> (right click, Save as..)<\/p>\n<p><strong>1: the PHP generating the avatar&#39;s URL<\/strong><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n$grav_default = &quot;http:\/\/yoursite.com\/images\/gravatar_default.gif&quot;;\r\n$grav_size = 40;\r\n$grav_email = $comment_author_email;\r\n$grav_url = &quot;http:\/\/www.gravatar.com\/avatar.php?gravatar_id=&quot; . md5($grav_email) . &quot;&amp;amp;default=&quot; . urlencode($grav_default) . &quot;&amp;amp;size=&quot; . $grav_size;\r\n?&gt;<\/code><\/pre><\/div>\n<p><strong>2: the PHP &#038; HTML used to display the avatar<\/strong><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-markup line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-markup\">&lt;div class=&quot;gravatar&quot;&gt;\r\n &lt;a title=&quot;Author&#039;s avatar&quot;&gt;\r\n  &lt;img src=&quot;http:\/\/yoursite.com\/images\/pixel.gif&quot; class=&quot;gravatar_img&quot;\r\n   style=&quot;background: url(&#039;&lt;?php echo $grav_url; ?&gt;&#039;);&quot;\r\n   alt=&quot;Author&#039;s avatar&quot; \/&gt;\r\n &lt;\/a&gt;\r\n&lt;\/div&gt;<\/code><\/pre><\/div>\n<p>That&#39;s the trick. The avatar <strong>img src<\/strong> is a transparent pixel, and the <strong>background<\/strong> is the real image. Should the server be unavailable (try replacing echo $grav_url with echo &quot;blah&quot;), it will show&#8230; nothing. Just the transparent pixel. And XHTML markup evangelists be not scared, it does validate.<\/p>\n<p><strong>Note<\/strong>: you can even set the default avatar to be a transparent pixel, so you will display no avatar for people not registered there. This trick ensures you to always display something proper for registered users, Gravatar.com being available or not.<\/p>\n<h2>Code for WordPress users<\/h2>\n<p>Happy users of <a href=\"http:\/\/wordpress.org\">WordPress<\/a>, here is the exact code you&#39;ll need.<\/p>\n<p>First, put the two needed images (<a href=\"\/blog\/images\/gravatar_default.gif\">gravatar_default.gif<\/a> and <a href=\"\/blog\/images\/pixel.gif\">pixel.gif<\/a>) in <strong><em>blogroot<\/em>\/wp-content\/themes\/default\/images\/<\/strong> (or in your own theme directory)<\/p>\n<p>Then edit <strong><em>blogroot<\/em>\/wp-content\/themes\/default\/comments.php<\/strong> (or, again, in your own theme directory) and locate the following line, around line 30 :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php foreach ($comments as $comment) : ?&gt;<\/code><\/pre><\/div>\n<p>Right after this one, put the following code :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-4\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n $grav_default = get_bloginfo(&#039;template_directory&#039;) . &quot;\/images\/gravatar_default.gif&quot; ;\r\n $grav_size = 40 ; \/\/ size of the image\r\n $grav_email = $comment-&gt;comment_author_email ;\r\n $grav_url = &quot;http:\/\/www.gravatar.com\/avatar.php?gravatar_id=&quot; . md5($grav_email) . &quot;&amp;amp;default=&quot; . urlencode($grav_default) . &quot;&amp;amp;size=&quot; . $grav_size ;\r\n?&gt;<\/code><\/pre><\/div>\n<p>Then, two lines below, locate the following code :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-5\"><pre class=\"language-markup line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-markup\">&lt;cite&gt;&lt;?php comment_author_link() ?&gt;&lt;\/cite&gt; Says:&lt;br \/&gt;<\/code><\/pre><\/div>\n<p>Right before it, add the following bits :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-6\"><pre class=\"language-markup line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-markup\">&lt;div class=&quot;gravatar&quot;&gt;\r\n&lt;a title=&quot;&lt;?php comment_author(); ?&gt;&quot;&gt;\r\n&lt;img  class=&quot;gravatar_img&quot; src=&quot;&lt;?php echo get_bloginfo(&#039;template_directory&#039;) ?&gt;\/images\/pixel.gif&quot;\r\n style=&quot;background: url(&#039;&lt;?php echo $grav_url; ?&gt;&#039;);&quot; alt=&quot;&lt;?php comment_author(); ?&gt;&quot; \/&gt;\r\n&lt;\/a&gt;\r\n&lt;\/div&gt;<\/code><\/pre><\/div>\n<p>Of course you&#39;ll have to style the result, using class <em>gravatar<\/em> and <em>gravatar_img<\/em> as provided in this example. And of course, I can be of no help on this matter :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implement Gravatar with no fear of the site being offline<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,55,94,10,245],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","tag-code","tag-css","tag-gravatar","tag-php","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}