1

有没有好的php自动头像生成?我见过很多像WP_MonsterIDWP_Identicon但都有 wordpress 插件。

4

4 回答 4

4

您可以使用Gravatar之类的东西,然后只需对电子邮件地址进行哈希处理,并让用户知道为了更改他们的头像,他们会前往 gravatar.com 进行更改。

比如做一个64像素的图片

<?
$email = "my@whatever.com";
$hash = md5(strtolower(trim($email)));
echo "<img src='http://gravatar.com/avatar/$hash?size=64&d=identicon' />
?>

您可以在此处查看有关此内容的更多文档:https ://en.gravatar.com/site/implement/images/

于 2012-09-30T08:27:08.567 回答
2

我定制了 wavatar wordpress 插件。我自定义的 wavaters.php 文件的内容:

//wavaters.php
define("AVATAR_SIZE",           '100');
define("WAVATAR_BACKGROUNDS",   '4');
define("WAVATAR_FACES",         '11');
define("WAVATAR_BROWS",         '8');
define("WAVATAR_EYES",          '13');
define("WAVATAR_PUPILS",        '11');
define("WAVATAR_MOUTHS",        '19');


/*-----------------------------------------------------------------------------
Clamp a value between 0 and 255
-----------------------------------------------------------------------------*/

function wavatar_clamp ($v)
{
    return $v < 0 ? 0 : ($v > 255 ? 255 : $v);
}

/*-----------------------------------------------------------------------------
Handy function for converting hus/sat/lum color values to RGB, which makes it
very easy to generate random-yet-still-vibrant colors.
-----------------------------------------------------------------------------*/

function wavatar_hsl ($h, $s, $l) 
{

    if ($h>240 || $h<0 || $s>240 || $s<0 || $l>240 || $l<0)
        return array(0,0,0);
    if ($h<=40) {
        $R=255;
        $G=(int)($h/40*256);
        $B=0;
    } elseif ($h>40 && $h<=80) {
        $R=(1-($h-40)/40)*256;
        $G=255;
        $B=0;
    } elseif ($h>80 && $h<=120) {
        $R=0;
        $G=255;
        $B=($h-80)/40*256;
    } elseif ($h>120 && $h<=160) {
        $R=0;
        $G=(1-($h-120)/40)*256;
        $B=255;
    } elseif ($h>160 && $h<=200) {
        $R=($h-160)/40*256;
        $G=0;
        $B=255;
    } elseif ($h>200) {
        $R=255;
        $G=0;
        $B=(1-($h-200)/40)*256;
    }
    $R=$R+(240-$s)/240*(128-$R);
    $G=$G+(240-$s)/240*(128-$G);
    $B=$B+(240-$s)/240*(128-$B);
    if ($l<120) {
        $R=($R/120)*$l;
        $G=($G/120)*$l;
        $B=($B/120)*$l;
    } else {
        $R=$l*((256-$R)/120)+2*$R-256;
        $G=$l*((256-$G)/120)+2*$G-256;
        $B=$l*((256-$B)/120)+2*$B-256;
    }
    return array((int)wavatar_clamp ($R),(int)wavatar_clamp($G),(int)wavatar_clamp($B));

}

/*-----------------------------------------------------------------------------
Helper function for building a wavatar.  This loads an image and adds it to 
our composite using the given color values.
-----------------------------------------------------------------------------*/

function wavatar_apply_image ($base, $part)
{

    $file = dirname(__FILE__).'/parts/' . $part . '.png';
    $im = @imagecreatefrompng($file);
    if(!$im)
        return;
    imagecopy($base,$im, 0, 0, 0, 0, AVATAR_SIZE, AVATAR_SIZE);
    imagedestroy($im);

}

/*-----------------------------------------------------------------------------
Builds the avatar.
-----------------------------------------------------------------------------*/

function wavatar_build ($seed, $filename, $size)
{

    //look at the seed (an md5 hash) and use pairs of digits to determine our
    //"random" parts and colors.
    $face =         1 + (hexdec (substr ($seed,  1, 2)) % (WAVATAR_FACES));
    $bg_color =         (hexdec (substr ($seed,  3, 2)) % 240);
    $fade =         1 + (hexdec (substr ($seed,  5, 2)) % (WAVATAR_BACKGROUNDS));
    $wav_color =        (hexdec (substr ($seed,  7, 2)) % 240);
    $brow =         1 + (hexdec (substr ($seed,  9, 2)) % (WAVATAR_BROWS));
    $eyes =         1 + (hexdec (substr ($seed, 11, 2)) % (WAVATAR_EYES));
    $pupil =        1 + (hexdec (substr ($seed, 13, 2)) % (WAVATAR_PUPILS));
    $mouth =        1 + (hexdec (substr ($seed, 15, 2)) % (WAVATAR_MOUTHS));
    // create backgound
    $avatar = imagecreatetruecolor (AVATAR_SIZE, AVATAR_SIZE);
    //Pick a random color for the background
    $c = wavatar_hsl ($bg_color, 240, 50);
    $bg = imagecolorallocate ($avatar, $c[0], $c[1], $c[2]);
    imagefill($avatar, 1, 1, $bg);
    $c = wavatar_hsl ($wav_color, 240, 170);
    $bg = imagecolorallocate ($avatar, $c[0], $c[1], $c[2]);
    //Now add the various layers onto the image
    wavatar_apply_image ($avatar, "fade$fade");
    wavatar_apply_image ($avatar, "mask$face");
    imagefill($avatar, (int)(AVATAR_SIZE / 2),(int)(AVATAR_SIZE / 2),$bg);
    wavatar_apply_image ($avatar, "shine$face");
    wavatar_apply_image ($avatar, "eyes$eyes");
    wavatar_apply_image ($avatar, "brow$brow");
    wavatar_apply_image ($avatar, "pupils$pupil");
    wavatar_apply_image ($avatar, "mouth$mouth");
    //resize if needed
    if ($size != AVATAR_SIZE) {
        $out = imagecreatetruecolor($size,$size);
        imagecopyresampled ($out,$avatar, 0, 0, 0, 0, $size, $size, AVATAR_SIZE, AVATAR_SIZE);
        imagepng($out, $filename);
        imagedestroy($out);
        imagedestroy($avatar);
    } else {
        imagepng($avatar, $filename);
        imagedestroy($avatar);
    }

}

要使用,请下载原版wavatar。解压parts文件夹,放到wavatar.php文件所在的目录下。然后像这样调用:

//$seed: $seed to use in create unique avatar for every user preferably md5 of the users email
//$filename: where and what name the image should be stored with
//$size: size of the image
function wavatar_build ($seed, $filename, $size);
于 2012-09-30T08:51:57.870 回答
1

WP_MonsterID 详细说明了它是如何工作的。以身作则。

于 2012-09-30T07:51:59.433 回答
0

您可以使用 imagecreatefrom*() 函数和其他相关函数为自己编写它。例如。imagecreatefrompng();

于 2012-09-30T07:46:21.897 回答