Disabling Favatars

23 December 2004 @ late evening | Comments (0)

Not long ago, I hacked a bit in Textpattern and added Favatar functionality to this site. I modified Paul James’ code slightly to add favicon caching, since most people only change their favicons once every blue moon.

Unfortunately, I think I’m going to remove Favatars. I like the idea in principle, but they caused pageload times to increase quite drastically with anything more than 5 or 10 comments. Articles with 45-50 comments would take upwards of 30-60s to load if pulling from an expired cache. So while it’s a nice novelty, I’ve decided (after as-of-yet no response from Paul James) to retire the feature temporarily.

I’m not exactly sure what’s going on, but I’ve read scattered reports of problems with fopen() and stream_set_timeout(). It seems that (perhaps only for non-existent favicons – but I haven’t bothered to fully test) the timeout is being completely ignored and PHP simply hangs on each request.

Anyways, I’ll post my slight modification here and receive criticism if anyone stumbles upon from Out There.

January 31, 2005: Just found Stuart Langridge’s Favatar methodology using Python CGI. I haven’t tried it out, but it looks promising.

March 14, 2005: ajw_favatar

  1. <?php
  2. // =====================================================
  3. // favatar.php (cached)
  4. //
  5. // Adapted from Paul James' code
  6. // - http://www.peej.co.uk/projects/favatars.html
  7. //
  8. // Returns -> URL or path/to/cached/file
  9. // =====================================================
  10.  
  11. function getFavicon($url) {
  12. $useCache= TRUE;
  13. $cacheTime= 86400; // seconds to expire
  14. $cacheDir= '/images/cache';
  15. $cacheFile= md5($url).'.ico';
  16. $cache= join( DIRECTORY_SEPARATOR, array( $_SERVER['DOCUMENT_ROOT'], $cacheDir, $cacheFile ));
  17. $cacheRel= join( DIRECTORY_SEPARATOR, array( $cacheDir, $cacheFile ));
  18. $url = strpos($url,'http://') ? $url : 'http://'.$url;
  19.  
  20. // Serve from the cache if it is younger than $cacheTime
  21. if (file_exists($cache) && time() - $cacheTime < filemtime($cache) && $useCache) {
  22. return $cacheRel;
  23. exit;
  24. }
  25.  
  26. // first request URL
  27. $HTTPRequest = @fopen($url, 'r');
  28. if ($HTTPRequest) {
  29. stream_set_timeout($HTTPRequest, 0, 500);
  30. $html = fread($HTTPRequest, 4096);
  31. $HTTPRequestData = stream_get_meta_data($HTTPRequest);
  32. fclose($HTTPRequest);
  33. if (!$HTTPRequestData['timed_out']) {
  34. if (preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
  35. $linkUrl = html_entity_decode($matches[1]);
  36. if (substr($linkUrl, 0, 1) == '/') {
  37. $urlParts = parse_url($url);
  38. $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
  39. } elseif (substr($linkUrl, 0, 7) == 'http://') {
  40. $faviconURL = $linkUrl;
  41. } elseif (substr($url, -1, 1) == '/') {
  42. $faviconURL = $url.$linkUrl;
  43. } else {
  44. $faviconURL = $url.'/'.$linkUrl;
  45. }
  46. } else {
  47. $urlParts = parse_url($url);
  48. $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
  49. }
  50. // then request favicon URL
  51. $HTTPRequest = @fopen($faviconURL, 'r');
  52. if ($HTTPRequest) {
  53. stream_set_timeout($HTTPRequest, 0, 500);
  54. $favicon = fread($HTTPRequest, 8192);
  55. $HTTPRequestData = stream_get_meta_data($HTTPRequest);
  56. fclose($HTTPRequest);
  57. if (!$HTTPRequestData['timed_out'] && strlen($favicon) < 8192) {
  58. if ($useCache) {
  59. // Cache the output to a file
  60. $fp = fopen($cache, 'w');
  61. fwrite($fp, $favicon);
  62. fclose($fp);
  63. }
  64. return $faviconURL;
  65. }
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. ?>
  72. Download this code: favatar.php.txt

Comments are closed for this article.

Previously