setEmail($email); if ($size) $this->setSize($size); if ($default) $this->setDefault($default); if ($rating) $this->setRating($rating); return $this; } /** * Set the email address to be used for the gravatar * * We assume the email address is valid and comes from a safe place. It is * not the role of this component to validate the email address. Passing * an invalid email address will most likely result in getting the default * Gravatar instead of a real one. * * @param string $email * @return Zend_View_Helper_Gravatar */ public function setEmail($email) { $this->_email = trim($email); return $this; } /** * Set the gravatar size * * @param integer $size Size in pixels between 1 and 512 * @return Zend_View_Helper_Gravatar */ public function setSize($size) { $size = (int) $size; if ($size < 1 || $size > 512) { require_once 'Zend/View/Exception.php'; throw new Zend_View_Exception("Gravatar size must be a number between 1 and 512"); } $this->_size = $size; return $this; } /** * Set the default gravatar URL or type * * Can be either an absolute URL to an image, or one of the DEFAULT_* constants * * @param string $default * @return Zend_View_Helper_Gravatar */ public function setDefault($default) { $this->_default = $default; return $this; } /** * Set the maximal rating (inclusive) of the Gravatar. * * Must be one of the RATING_* constants * * @param string $rating Rating - can be g, pg, r or 'x'. * @return Zend_View_Helper_Gravatar */ public function setRating($rating) { $rating = strtolower($rating); switch($rating) { case self::RATING_G: case self::RATING_PG: case self::RATING_R: case self::RATING_X: $this->_rating = $rating; break; default: require_once 'Zend/View/Exception.php'; throw new Zend_View_Exception("Gravatar rating must be one of g, pg, r or x"); } return $this; } /** * Get a full tag with the Gravatar URL, as opposed to just a URL * * You should pass additional img tag attributes (namely 'alt') as an * associative array * * @param array $attribs * @return Zend_View_Helper_Gravatar */ public function getImageTag(array $attribs = array()) { return '_htmlAttribs($attribs) . $this->getClosingBracket(); } /** * Stringify the Gravatar object - this returns a gravat URL * * @return string */ public function __toString() { return $this->_getGravatarUrl(); } /** * Generate the full gravatar URL * * @return string */ protected function _getGravatarUrl() { if (! $this->_email) { require_once 'Zend/View/Exception.php'; throw new Zend_View_Exception("No Gravatar email address was set"); } $email = md5(strtolower($this->_email)); $url = $this->_serviceUrl . $email . "?s={$this->_size}" . "&d=" . urlencode($this->_default) . "&r={$this->_rating}"; return $url; } }