In: , , , ,
On: 2005 / 03 / 20 Viewed: 74000 times
Shorter URL for this post: http://ozh.in/54

I'm currently redesigning this site and I think I'm going to give Gravatar a go. Something I'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 broken image icon.

So, I've come up with a simple and CSS way to avoid this. Display a proper avatar when available, display no image when offline.

Wait, wait. Gra-va-what ?

Gravatar is a free centralized database of avatars, hence its name : globally recognized avatar. On all the sites and weblogs using Gravatar, the same avatar can impersonnify you. And there are more and more weblogs using Gravatar, including very popular ones.

In one word : it's cool. Can we go on ? :)

The problem

Sometimes a website can become unavailable. Trying to display an image from this server only shows a tiny red cross, also known as "the broken image icon". While it can sometimes actually be pretty, most of the times, it sucks. Firefox users have some options to avoid this (in about:config set browser.display.show_image_placeholders to false) but as far as I know, there no such thing in MSIE.

The solution

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 original code. The paths and vars used here are "generic", you'll have to figure out how to adapt this for your blog on your own.

You'll need two images :

  1. a default avatar, used when the commenters did not register with Gravatar, named gravatar_default.gif here
  2. a transparent image, like a single transparent pixel, named pixel.gif here

The uber lazy bloggers can steal mine : gravatar_default.gif and pixel.gif (right click, Save as..)

1: the PHP generating the avatar's URL

  1. <?php
  2. $grav_default = "http://yoursite.com/images/gravatar_default.gif";
  3. $grav_size = 40;
  4. $grav_email = $comment_author_email;
  5. $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5($grav_email) . "&amp;default=" . urlencode($grav_default) . "&amp;size=" . $grav_size;
  6. ?>

2: the PHP & HTML used to display the avatar

  1. <div class="gravatar">
  2.  <a title="Author's avatar">
  3.   <img src="http://yoursite.com/images/pixel.gif" class="gravatar_img"
  4.   style="background: url('<?php echo $grav_url; ?>');"
  5.    alt="Author's avatar" />
  6.  </a>
  7. </div>

That's the trick. The avatar img src is a transparent pixel, and the background is the real image. Should the server be unavailable (try replacing echo $grav_url with echo "blah"), it will show… nothing. Just the transparent pixel. And XHTML markup evangelists be not scared, it does validate.

Note: 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.

Code for WordPress users

Happy users of WordPress, here is the exact code you'll need.

First, put the two needed images (gravatar_default.gif and pixel.gif) in blogroot/wp-content/themes/default/images/ (or in your own theme directory)

Then edit blogroot/wp-content/themes/default/comments.php (or, again, in your own theme directory) and locate the following line, around line 30 :

  1. <?php foreach ($comments as $comment) : ?>

Right after this one, put the following code :

  1. <?php
  2.  $grav_default = get_bloginfo('template_directory') . "/images/gravatar_default.gif" ;
  3.  $grav_size = 40 ; // size of the image
  4.  $grav_email = $comment->comment_author_email ;
  5.  $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5($grav_email) . "&amp;default=" . urlencode($grav_default) . "&amp;size=" . $grav_size ;
  6. ?>

Then, two lines below, locate the following code :

  1. <cite><?php comment_author_link() ?></cite> Says:<br />

Right before it, add the following bits :

  1. <div class="gravatar">
  2. <a title="<?php comment_author(); ?>">
  3. <img  class="gravatar_img" src="<?php echo get_bloginfo('template_directory') ?>/images/pixel.gif"
  4.  style="background: url('<?php echo $grav_url; ?>');" alt="<?php comment_author(); ?>" />
  5. </a>
  6. </div>

Of course you'll have to style the result, using class gravatar and gravatar_img as provided in this example. And of course, I can be of no help on this matter :)

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/54

Metastuff

This entry "Gravatar and the Broken Image Icon" was posted on 20/03/2005 at 4:12 pm and is tagged with , , , ,
Watch this discussion : Comments RSS 2.0.

14 Blablas

  1. BB2k says:

    Je vais te laisser le soin de tester avant de m'y mettre alors =)

  2. Ozh says:

    Bah c'est testé déjà, c'est dans mon redesign que je termine avant de mettre sur le site.

  3. Korbo says:

    Not bad, not bad… Didn't think of the broken image, I'll update my php. Just avoid empty gifs: who needs server round-trips for one or more 1px gifs that are transparent? If you're going for a bg image, go for it the whole way! Add the gravatar as a background to the gravatar container, one way or another. But that's gonna prove tricky in css… Hum. (Thinking). Check back on http://goulvench.free.fr/index.php?id=a236 in a cpl of days to see if I find a solution.

  4. Korbo says:

    Nice sniffing btw, but I'm French. Just happen to be in Sweden, so don't rely on that! :P

  5. […] —た。Gravatarが落ちていると自分のアバターも表示されないようで、これを解決するプラグインや〠[…]

  6. […] Gravatar implementation with PHP and CSS – The Raven Ground Autograv – I am Morgan Knutson Gravatar and the Broken Image Icon – Planet Ozh Identity […]

  7. […] Gravatar implementation with PHP and CSS – The Raven Ground Autograv – I am Morgan Knutson Gravatar and the Broken Image Icon – Planet Ozh Identity […]

  8. usayd says:

    Thats very cool…but wheres the avatars

  9. anu says:

    nice site.

  10. Pras says:

    Where to get the transparent image?

  11. […] following sites might be of use for me to get this happening: Gravatar and the Broken Image Icon Gravatar Popup Plugin CSS Styling for Alternating Colours & Author Comment Background […]

  12. […] have included the code for that which i followed the steps given by this guy Code Unfortunately it isn't working. So gotta […]

  13. Replica watches,Siwss watches,Japan watches,Rolex watches,Omega,Gucci,Louis Vuitton,watches,Breitling,Vacheron Constantin,Chanel,Cartier,Bvlgari,Jacob & Co,Chopard,D&G watches Alain Silberstein,A Lange & Sohne,Porsche Design Watches,Mont Blanc,Patek Philippe,TAG Heuer,Jaeger,LeCoultre,pretty watches,high quality watches,fake watches
    http://onestoptown.com

  14. jeremyclarke says:

    Holy shit, this is ingenious but so simple at the same time. Looking at the source of the core get_avatar() function it seems like they still aren't using this method, but just using the image directly… This seems like a MUCH better idea. I'll have to use this instead if we switch to Grav (i'm sick of dealing with avatars locally, what a headache).

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?