Skip to content

Instantly share code, notes, and snippets.

@dofy
Last active August 29, 2015 14:02
Show Gist options
  • Save dofy/6c2cfc5597fbeca6d011 to your computer and use it in GitHub Desktop.
Save dofy/6c2cfc5597fbeca6d011 to your computer and use it in GitHub Desktop.
Face kit, Enjoy~~
<?php
/**
* Author: Seven Yu
* E-Mail: dofyyu (a.t) gmail.com
* Version: 0.0.3
* Update: 2012/11/14
*/
$rand = true;
$cache = true;
$time = $rand ? time() : 0;
$name = isset($_GET['name']) ? trim($_GET['name']) : $time;
$name = empty($name) ? $time : $name;
$code = isset($_GET['code']) ? $_GET['code'] : md5($name);
if(preg_match('/^[0-9a-f]{32}$/i', $code) == 0)
{
$code = md5($time);
}
$assets = array(
array('name' => 'bg', 'count' => 30),
array('name' => 'face', 'count' => 10),
array('name' => 'eye', 'count' => 11),
array('name' => 'nose', 'count' => 10),
array('name' => 'mouth', 'count' => 10),
array('name' => 'hair', 'count' => 5),
);
$folder1 = substr($code, 0, 2);
$folder2 = substr($code, 2, 2);
$folder = "cache/$folder1/$folder2";
$file = "$folder/$code.png";
if(!$cache || !file_exists($file))
{
// make image
$i = 0;
$parts = str_split($code, 5);
$im = imagecreatetruecolor(64, 64);
foreach($assets as $item)
{
$name = $item['name'];
$index = getWordOrd($parts[$i++]) % $item['count'];
$img = "assets/$name/$index.png";
if(file_exists($img))
{
$add = imagecreatefrompng($img);
imagecopy($im, $add, 0, 0, 0, 0, 64, 64);
}
}
if($cache)
{
if(!file_exists($folder))
{
mkdir($folder, 0777, true);
}
imagepng($im, $file);
}
else
{
header('Content-Type: image/png');
imagepng($im);
}
imagedestroy($im);
}
$cache && header("location:$file");
/**
* 单词 ascii 码和
* @param $word 字符串
* @return 求和
* */
function getWordOrd($word)
{
$result = 0;
foreach(str_split($word) as $char)
{
$result += ord($char);
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment