-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathavatar.php
54 lines (45 loc) · 1.53 KB
/
avatar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
build_monster($_REQUEST['seed'],$_REQUEST['size']);
function build_monster($seed='',$size=''){
// init random seed
if($seed) srand( hexdec(substr(md5($seed),0,6)) );
// throw the dice for body parts
$parts = array(
'body' => rand(1,2),
'hands' => rand(1,2),
'eyes' => rand(1,2),
'mouth' => rand(1,2),
'accessorie' => rand(1,2),
'tentacles' => rand(1,2)
);
// create backgound
$monster = @imagecreatetruecolor(256, 256)
or die("GD image create failed");
$white = imagecolorallocate($monster, 255, 255, 255);
imagefill($monster,0,0,$white);
// add parts
foreach($parts as $part => $num){
$file = dirname(__FILE__).'/img/'.$part.'_'.$num.'.png';
$im = @imagecreatefrompng($file);
if(!$im) die('Failed to load '.$file);
imageSaveAlpha($im, true);
imagecopy($monster,$im,0,0,0,0,256,256);
imagedestroy($im);
}
// restore random seed
if($seed) srand();
// resize if needed, then output
if($size && $size < 400){
$out = @imagecreatetruecolor($size,$size)
or die("GD image create failed");
imagecopyresampled($out,$monster,0,0,0,0,$size,$size,256,256);
header ("Content-type: image/png");
imagepng($out);
imagedestroy($out);
imagedestroy($monster);
}else{
header ("Content-type: image/png");
imagepng($monster);
imagedestroy($monster);
}
}