Outils pour utilisateurs

Outils du site


php:cache_gravatar

Ceci est une ancienne révision du document !


Cache local de Gravatars

Code PHP pour faire un cache de Gravatar. Utilité ? Éviter que Gravatar puisse tracer les internautes, que le site soit bousillé si Gravatar est injoignable, … Les commentaires sont en anglais.

get.php
<?php 
/*
How to use it:
call it from your website like this:
http://www.yoursite.com/some_folder_for_gravatar_caching/get.php?g={md5_from_email}
*/
$expire = time() -604800 ;  // default: 604800 (7 days)
if (isset($_GET['g']))  // g given ? if yes...
{
if (strlen($_GET['g']) !== 32) { die; }  // g is 32 character long ? if no, die.
$hash = ereg_replace("[^a-f0-9]", "", $_GET['g'] );  // strip out anything that doesn't belong in a md5 hash
if (strlen($hash) != 32) { die; }  // still 32 characters ? if no, given hash wasn't genuine. die.
$newfile = $hash.'.png';
$file = 'https://secure.gravatar.com/avatar/'.$hash.'?s=60&d=monsterid';  // gravatar, 60px monsterid.
if(file_exists($newfile) && filemtime($newfile) < $expire)
{ unlink($newfile); }  // expired gravatar, out !
if (file_exists($newfile))
{ }  // the gravatar wasn't removed before: it's valid and doesn't need refreshment
else
{ copy($file, $newfile);   // gravatar deleted, getting new.
$imagecheck = getimagesize($newfile);
if ($imagecheck['mime']!=='image/png')  // is it a PNG ?
  {   
imagepng(imagecreatefromjpeg($newfile),$newfile.'2');  // if no, creating PNG and replacing
unlink($newfile);
rename($newfile.'2', $newfile);
  }
}
header('Location: '.$newfile.''); }   // and finally let's redirect to the cached gravatar.
else
{ header("HTTP/1.0 404 Not Found"); echo "erreur"; }  // g not given, return error.
?>
php/cache_gravatar.1338234975.txt.gz · Dernière modification : 2013-02-19 20:28 (modification externe)