Posted By

xuanyan on 08/27/08


Tagged

class php5 Gravatar kukufun xuanyan


Versions (?)

Gravatar link generator class


 / Published in: PHP
 

URL: http://www.kukufun.com

Gravatar link generator class

  1. <?php
  2.  
  3. /**
  4.  * Gravatar link generator class
  5.  *
  6.  * @author xuanyan <[email protected]>
  7.  * @link http://www.kukufun.com
  8.  */
  9.  
  10. // example1:
  11. // $gravatar = new Gravatar('[email protected]');
  12. // $gravatar->setDefault('monsterid')->setSize(100);
  13. // echo $gravatar;
  14. //
  15. // example2:
  16. // $url = Gravatar::creat('[email protected]')
  17. // ->setDefault('http://www.kukufun.com/images/logo.png')
  18. // ->setRating('x')
  19. // ->__toString();
  20.  
  21.  
  22.  
  23. class Gravatar
  24. {
  25. private static $default_array = array('identicon', 'wavatar', 'monsterid');
  26. private static $rating_array = array('g', 'pg', 'r', 'x');
  27.  
  28. private $size = 80;
  29. private $rating = 'g';
  30. private $default = '';
  31. private $email = '';
  32. const URL = 'http://www.gravatar.com/avatar/';
  33.  
  34. public function __construct($email = '')
  35. {
  36. $this->email = md5(trim((string)$email));
  37. }
  38.  
  39. public static function creat($email = '')
  40. {
  41. return new self($email);
  42. }
  43.  
  44. public function setRating($rating = 'g')
  45. {
  46. if (in_array($rating, self::$rating_array))
  47. {
  48. $this->rating = $rating;
  49. }
  50.  
  51. return $this;
  52. }
  53.  
  54. public function setDefault($default = '')
  55. {
  56. $default = trim($default);
  57. if (substr($default, 0, 4) == 'http')
  58. {
  59. $this->default = urlencode($default);
  60. }
  61. elseif (in_array($default, self::$default_array))
  62. {
  63. $this->default = $default;
  64. }
  65.  
  66. return $this;
  67. }
  68.  
  69. public function setSize($size = 80)
  70. {
  71. $size = intval($size);
  72. $size < 1 && $size = 1;
  73. $size > 512 && $size = 512;
  74. $this->size = $size;
  75.  
  76. return $this;
  77. }
  78.  
  79. public function __toString()
  80. {
  81. $result = self::URL . $this->email . '?s=' . $this->size . '&r=' .$this->rating;
  82. $this->default && $result .= '&d=' . $this->default;
  83.  
  84. return $result;
  85. }
  86. }
  87. ?>

Report this snippet  

You need to login to post a comment.