I'm stuck on this. This code pulls a gravatar image for the $source argument. I'm trying to determine how to pass the url to gravatar and determine if the result is an image or a 404.
If its a 404, I don't want to copy default.png into a new image, I just want to use the default.png
function cb_gravatar($source) {
$time = 1209600; //The time of cache(seconds)
preg_match('/avatar\/([a-z0-9]+)\?s=(\d+)/', $source, $tmp);
$abs = TEMPLATEPATH.
'/gravatar-cache/'.$tmp[1].
'.jpg';
$url = get_bloginfo('template_directory').
'/gravatar-cache/'.$tmp[1].
'.jpg';
$default = get_bloginfo('template_directory').
'/gravatar-cache/'.
'default.png';
if (!is_file($abs) || (time() - filemtime($abs)) > $time) {
//copy('http://www.gravatar.com/avatar/'.$tmp[1].'?s=64&d='.$default.'&r=G',$abs);
//IF REQUEST RETURNS A 404 USE THE LOCAL DEFAULT.PNG
copy('http://www.gravatar.com/avatar/'.$tmp[1].
'?s=32&d=404&r=G', $abs);
}
if (filesize($abs) < 500) {
copy($default, $abs);
}
return '<img alt="" src="'.$url.
'" class="avatar avatar-'.$tmp[2].
'" width="'.$tmp[2].
'" height="'.$tmp[2].
'" />';
}
Gravatar have added an option to the 'd' parameter, meaning that if you pass in d=404, you get a 404 page (instead of some 302 redirect to a default picture) if there's no picture, rather than having to use heuristics.
Though you said you know about the d parameter, do you know it actually returns a redirect header when applicable? So, the following yields 302 Found because the avatar does not exist:
HTTP/1.1 302 Found
...
Last-Modified: Wed, 11 Jan 1984 08:00:00 GMT
Location: http://www.google.com/images/logo.gif
Content-Length: 0
...
Expires: Sun, 26 Jul 2009 23:18:33 GMT
Cache-Control: max-age=300
Seems to me that all you need to do is add that d parameter and check the HTTP result code then.