-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.php
executable file
·32 lines (29 loc) · 1.04 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
<?php
function create($string, $blocks = 5, $size = 400)
{
$togenerate = ceil($blocks / 2);
$hash = md5($string);
$hashsize = $blocks * $togenerate;
$hash = str_pad($hash, $hashsize, $hash);
$color = substr($hash, 0, 6);
$blocksize = $size / $blocks;
$image = imagecreate($size, $size);
$bg = imagecolorallocate($image, 255, 255, 255);
$color = imagecolorallocate($image, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));
for ($x = 0; $x < $blocks; $x++)
{
for ($y = 0; $y < $blocks; $y++)
{
if ($x < $togenerate)
$pixel = hexdec($hash[$x * $blocks + $y]) % 2 == 0;
else
$pixel = hexdec($hash[(($blocks - 1) - $x) * $blocks + $y]) % 2 == 0;
$pixelcolor = $bg;
if ($pixel)
$pixelcolor = $color;
imagefilledrectangle($image, $x * $blocksize, $y * $blocksize, ($x + 1) * $blocksize, ($y + 1) * $blocksize, $pixelcolor);
}
}
header('Content-type: image/png');
imagepng($image);
}