Cache favicon from google service


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //get domain from url
  2. function get_domain($url) {
  3. /*
  4. Author : Ayush
  5. URL : http://Webgarb.com
  6. */
  7. $url = explode('/', str_replace('www.', '', str_replace('http://', '', $url)));
  8. return $url[0];
  9. }
  10.  
  11. //get favicon from google and cache it
  12. function getFavicon($var){
  13. $cache_days = 30;
  14. $linkdomain = get_domain($var);
  15. $favicon_cache_dir = ABSPATH.'wp-content'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'favicon_cache';
  16. $favicon_url = 'http://www.quakemachinex.com/blog/wp-content/plugins/favicon_cache/'.$linkdomain.'.png';
  17. $fp = fopen ($favicon_cache_dir.'/'.$linkdomain.'.png', 'r');
  18. $favicon_create_time = filectime($favicon_cache_dir.'/'.$linkdomain.'.png');
  19. $time_now = date("Y-m-d H:i:s");
  20. $favicon_stay_days = (strtotime($time_now)-strtotime(date("Y-m-d H:i:s",$favicon_create_time)))/86400;
  21. $favicon_stay_days = floor($favicon_stay_days);
  22.  
  23. if ($fp && $favicon_stay_days < $cache_days) {
  24. return $favicon_url;
  25. } else {
  26. $remote_favicon = file_get_contents('http://www.google.com/s2/favicons?domain='.$linkdomain);
  27. $local_favicon = $favicon_cache_dir.'/'.$linkdomain.'.png';
  28. $local_favicon_actual = fopen($local_favicon, 'w+');
  29. fwrite($local_favicon_actual, $remote_favicon);
  30. fclose($local_favicon_actual);
  31. return $favicon_url;
  32. }
  33.  
  34. }

URL: http://www.quakemachinex.com/blog/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.