Gravatars, and how not to show them

Nov 18 2004

Bandwagon-jumper that I am, I’ve now enabled Gravatars on this site. If you have one of these personal blog icons set up for the email address you use to comment, it will show up next to your comments.

The basic implementation instructions don’t mention how to not display the gravatar for those commenters who don’t have one, so here is how I accomplished it.

The only way to tell that a person doesn’t have an avatar is to check the dimensions of the image returned by the function call – if it is a 1×1 .gif, then no ‘official’ image is stored. To check the dimensions we can use the PHP getimagesize() function, which creates an array containing the width, height, type and attributes of the image. By checking either the width or height we can establish whether there is an image to display or not:

<?php
$gravatar = getimagesize("<txp:gravatar />");
if ($gravatar[ 0 ] != 1) echo "<img 
src="<txp:gravatar />&size=50" 
alt="Commenter's gravatar" class="gravatar" />";
?>

This is obviously only relevant to Textpattern blogs, but could be used on any PHP-based system. Note that the spaces inside the square brackets should be removed.

Filed under: Internet, Textpattern.

Digg this article

Bookmark this article with del.icio.us

Previously: Support the Right to Read

Next: Ungoogled


Comments

Stuart
1388 days ago
This looks rather good. I hadn’t spotted this one before. I’ve been trying to implement smilies but I might not bother now. I have to say that I understood the “hack” bit but they lost me a little on the usage. Am I right in thinking that their tag goes into the “comment” form as opposed to the “comment_form” form if you understand the difference and then do I presume that your bit of PHP above replaces their tag? And then again does the “size” attribute in your PHP or their tag reduce or enlarge anyone’s avatar to give a uniform size? And what’s a “G” rating? Do I ask too many questions?
#1
Matthew Pennell
1388 days ago
Yes, their tag goes into the ‘comment’ form, and my PHP replaces it altogether.

The size attribute resizes any gravatar (bicubically, apparently – don’t ask me what that means), and the ratings are the same as the US cinema ratings; they allow you to prevent any explicit gravatars from being displayed on your site.
#2
Stuart
1388 days ago
I’ll have to look at your PHP later today. I’m out and about a lot until the evening.
#3
Jonathan M. Hollin
1387 days ago
What a coincidence!

I made my comments system Gravatar-friendly a couple of days ago Matthew.

Today, I was looking through comments on some of my older posts to see if any of the posters had created a Gravatar. Yours was the only one I have found (so far).

So I clicked through to your site and the first thing I see is a post about Gravatars.

Welcome to the bandwagon mate! :-)
#4
Jonathan M. Hollin
1387 days ago
“bicubically, apparently dont ask me what that means”

I think it just means that you supply a single size value and the value is applied to both image dimensions (width & height).

It’s certainly not a dictionary word though!
#5
Matthew Pennell
1386 days ago
I see Richard Rutter over at Clagnut has nicked my PHP idea!
#6
Mathias Bynens
1386 days ago
You know what they say—- great minds think alike :)
#7
Stuart
1385 days ago
Is it me or is the sevice a little flakey?
#8
Matthew Pennell
1385 days ago
Not noticed it myself – you have broken my layout though; make longer comments!
#9
Paul Armstrong
1342 days ago
What would be the best way to give people a default image if they don’t already have one?
#10
Matthew Pennell
1342 days ago
Paul – luckily there is very little you have to do to show a default image; simply include a ‘default’ parameter in the query string pointing at your chosen default image:

&default=http%3A%2F%2Fwww.somesite.com%2Fsomeimage.jpg

Notice that you have to escape the special characters within the image URL.

Full instructions can be found at the official Gravatar site. Rob’s Zooiblog is a good example of using default images – note the “Null” image.
#11
Paul Armstrong
1331 days ago
Instead of doing that, since I am using Jon Hicks’ method of even and odd comment listings, I have two different background images for the gravatars. That way, there is some more randominity to the images when people don’t have gravatars.
#12