[PHP] – Menggunakan dan Menempelkan Gravatar di Website sendiri

Untuk yang suka nge-blog, aktif di forum mungkin sudah tidak asing lagi dengan istilah AVATAR , gambar yang muncul pada halaman web setiap kali memasukan komentar, reply post, atau bahkan nge-twit ? Biasanya, mungkin untuk pembuat website yang menggunakan CMS WordPress sudah terbiasa, karena terhubung dengan account gravatar ‘terkadang’ secara otomatis, tapi bagaimana jika kita membuat website sendiri, yang memiliki fasilitas komentar artikel dan ingin adanya avatar disetiap orang, dimana orang tersebut memiliki account gravatar ??

Contoh Avatar

Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. Avatars help identify your posts on blogs and web forums, so why not on any site?

Hal yang pertama kita harus lakukan ada mendaftarkan diri di website https://en.gravatar.com/site/signup ,

catatan : Seperti yang saya bilang, Saat kita mendaftar di gravatar, terkadang atau memang, berarti kita sudah otomatis terdaftar di wordpress.com.

Berikut adalah langkah-langkahnya :

Step – 1 : Daftarkan Account dengan memasukan email terlebih dahulu.

Step – 2 : Gravatar mengirimkan konfimasi email untuk aktivasi

Step – 3 : Email dari Gravatar berupa Link Konfirmasi

Step – 4 : Verifikasi Email berhasil dan account sudah aktif.

Step – 5 : Masuk Ke wordpress dengan Email yang didaftarkan di gravatar

Step – 6 : Setting Gravatar untuk profile WordPress

Step – 7 : Upload gambar dari komputer lokal

Step 8 : Pilih Gambar yang akan dijadikan sebagai avatar

Step 9 : Atur besar (lebar-tinggi) gambar yang ingin dijadikan avatar

Step 10: Pilih Rating sesuai keinginan (sesuai jenis website)

Step 11: Konfirmasi Avatar. Finish

Setelah mengikuti langkah-langkah diatas, kita akan sedikit menyisipkan class yang dimiliki dan diberikan gratis agar bisa diakses secara public, sehingga hasilnya seperti ini :

Hasil mengakses Avatar menggunakan Hash Request

sebetulnya ada beberapa cara, diantaranya dengan link public avatar :

  • Membuat Hash http://en.gravatar.com/site/implement/hash/
  • Gravatar Image Requests
  • Gravatar Profile Requests
    // pattern code http://www.gravatar.com/avatar/HASH
    // http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50 <-- hash email terdaftar
    // <img src="http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50">
    <?php
    echo md5( strtolower( trim( "citstudio.bdg@gmail.com" ) ) ); // d5321102792df3542ba563cad82f434e
    echo '<br/>';
    ?>
    <img src="http://www.gravatar.com/avatar/d5321102792df3542ba563cad82f434e" alt="" />
    

atau dengan class yang diberikan gravatar.. :D

<?php

/**
* Nama File : gravatar.class.php
*
* Class Gravatar
*
* From Gravatar Help:
*        "A gravatar is a dynamic image resource that is requested from our server. The request
*        URL is presented here, broken into its segments."
* Source:
*    http://site.gravatar.com/site/implement
*
* Usage:
* <code>
*        $email = "youremail@yourhost.com";
*        $default = "http://www.yourhost.com/default_image.jpg";    // Optional
*        $gravatar = new Gravatar($email, $default);
*        $gravatar->size = 80;
*        $gravatar->rating = "G";
*        $gravatar->border = "FF0000";
*
*        echo $gravatar; // Or echo $gravatar->toHTML();
* </code>
*
*    Class Page: http://www.phpclasses.org/browse/package/4227.html
*
* @author Lucas Araújo <araujo.lucas@gmail.com>
* @version 1.0
* @package Gravatar
*/
class Gravatar
{
    /**
     *    Gravatar's url
     */
    const GRAVATAR_URL = "http://www.gravatar.com/avatar.php";

    /**
     *    Ratings available
     */
    private $GRAVATAR_RATING = array("G", "PG", "R", "X");

    /**
     *    Query string. key/value
     */
    protected $properties = array(
        "gravatar_id"    => NULL,
        "default"        => NULL,
        "size"            => 80,        // The default value
        "rating"        => NULL,
        "border"        => NULL,
    );

    /**
     *    E-mail. This will be converted to md5($email)
     */
    protected $email = "";

    /**
     *    Extra attributes to the IMG tag like ALT, CLASS, STYLE...
     */
    protected $extra = "";

    /**
     *
     */
    public function __construct($email=NULL, $default=NULL) {
        $this->setEmail($email);
        $this->setDefault($default);
    }

    /**
     *
     */
    public function setEmail($email) {
        if ($this->isValidEmail($email)) {
            $this->email = $email;
            $this->properties['gravatar_id'] = md5(strtolower($this->email));
            return true;
        }
        return false;
    }

    /**
     *
     */
    public function setDefault($default) {
        $this->properties['default'] = $default;
    }

    /**
     *
     */
    public function setRating($rating) {
        if (in_array($rating, $this->GRAVATAR_RATING)) {
            $this->properties['rating'] = $rating;
            return true;
        }
        return false;
    }

    /**
     *
     */
    public function setSize($size) {
        $size = (int) $size;
        if ($size <= 0)
            $size = NULL;        // Use the default size
        $this->properties['size'] = $size;
    }

    /**
     *
     */
    public function setExtra($extra) {
        $this->extra = $extra;
    }

    /**
     *
     */
    public function isValidEmail($email) {
        // Source: http://www.zend.com/zend/spotlight/ev12apr.php
        return eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email);
    }

    /**
     *    Object property overloading
     */
    public function __get($var) { return @$this->properties[$var]; }

    /**
     *    Object property overloading
     */
    public function __set($var, $value) {
        switch($var) {
            case "email":    return $this->setEmail($value);
            case "rating":    return $this->setRating($value);
            case "default":    return $this->setDefault($value);
            case "size":    return $this->setSize($value);
            // Cannot set gravatar_id
            case "gravatar_id": return;
        }
        return @$this->properties[$var] = $value;
    }

    /**
     *    Object property overloading
     */
    public function __isset($var) { return isset($this->properties[$var]); }

    /**
     *    Object property overloading
     */
    public function __unset($var) { return @$this->properties[$var] == NULL; }

    /**
     *    Get source
     */
    public function getSrc() {
        $url = self::GRAVATAR_URL ."?";
        $first = true;
        foreach($this->properties as $key => $value) {
            if (isset($value)) {
                if (!$first)
                    $url .= "&";
                $url .= $key."=".urlencode($value);
                $first = false;
            }
        }
        return $url;
    }

    /**
     *    toHTML
     */
    public function toHTML() {
        return     '<img src="'. $this->getSrc() .'"'
                .(!isset($this->size) ? "" : ' width="'.$this->size.'" height="'.$this->size.'"')
                .$this->extra
                .' />';
    }

    /**
     *    toString
     */
    public function __toString() { return $this->toHTML(); }
}

?>

Nama File : index.php

MENGGUNAKAN CLASS<br/>
<?php
 * include "gravatar.class.php";

		// native function
		/**
 * Get either a Gravatar URL or complete image tag for a specified email address.
 *
 * @param string $email The email address
 * @param string $s Size in pixels, defaults to 80px [ 1 - 512 ]
 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
 * @param boole $img True to return a complete IMG tag False for just the URL
 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
 * @return String containing either just a URL or a complete image tag
 * @source http://gravatar.com/site/implement/images/php/
 */
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
	$url = 'http://www.gravatar.com/avatar/';
	$url .= md5( strtolower( trim( $email ) ) );
	$url .= "?s=$s&d=$d&r=$r";
	if ( $img ) {
		$url = '<img src="' . $url . '"';
		foreach ( $atts as $key => $val )
			$url .= ' ' . $key . '="' . $val . '"';
		$url .= ' />';
	}
	return $url;
}

		// class
		$email = "citstudio.bdg@gmail.com";			 	// email account @gravatar
        $default = "http://www.citstudio.com/";     // Optional
        $gravatar = new Gravatar($email, $default);
        $gravatar->size = 80;
        $gravatar->rating = "G";
        $gravatar->border = "FF0000";

        echo $gravatar."<br/><br/>";
		echo $gravatar->toHTML()."<br/>";

		// native
		$email = "citstudio.bdg@gmail.com";
		$default = "http://www.citstudio.com/homestar.jpg";
		$size = 40;
		$grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size;
?>
MENGGUNAKAN FUNCTION<br/>
<img src="<?php echo get_gravatar('citstudio.bdg@gmail.com');?>"/><br/>
TANPA FUNCTION<br/>
<img src="<?php echo $grav_url; ?>" alt="" /><br/>

<?php
echo md5( strtolower( trim( "citstudio.bdg@gmail.com" ) ) ); // d5321102792df3542ba563cad82f434e
echo '<br/>';
?>
<img src="http://www.gravatar.com/avatar/d5321102792df3542ba563cad82f434e" alt="" />

HAPPY PHP’ING ^_^

Fork me on GitHub

Comments

comments

Powered by Facebook Comments