-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
84 lines (73 loc) · 2.42 KB
/
renderer.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/* Render, please PHP by DevJjck, 2024
Original Render, please by Lunnaholy */
class Renderer
{
public function __construct($x = 0, $y = 0)
{
$this->startX = $x;
$this->startY = $y;
}
protected function hexColorAllocate($im, $hex)
{
$hex = ltrim($hex, '#');
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
return imagecolorallocate($im, $r, $g, $b);
}
public function render($ava, $type = 'jpeg', $file = null)
{
if (empty($ava))
throw new Exception('Avatar cannot be empty');
if ($type == 'base64' && !empty($saveTo))
throw new Exception('Cannot save base64 image to file');
$ava = str_replace("<UserAvatar><pixels>", "", $ava);
$ava = str_replace("</pixels></UserAvatar>", "", $ava);
$canvas = $this->processRender($ava);
if (empty($file))
ob_start();
switch ($type) {
case 'jpeg':
case 'jpg':
$result = imagejpeg($canvas, $file, 100);
break;
case 'png':
$result = imagepng($canvas, $file, 0);
break;
case 'base64':
$result = imagepng($canvas, $file, 0);
break;
default:
throw new Exception('unknown image type');
}
if (empty($file)) {
$image = ob_get_contents();
ob_end_clean();
imagedestroy($canvas);
return $type !== 'base64' ? $image : base64_encode($image);
} else {
return $result;
}
}
protected function processRender($ava)
{
$canvas = imagecreatetruecolor(189, 219);
$x = $this->startX;
$y = $this->startY;
for ($index = 4; $index < strlen($ava); $index += 6) {
$hexcolor = substr($ava, $index + 4, 2) . substr($ava, $index + 2, 2) . substr($ava, $index, 2);
$color = $this->hexColorAllocate($canvas, $hexcolor);
imagesetpixel($canvas, $x, $y, $color);
$x++;
if ($x == 189) {
// go to new line
$x = 0;
$y++;
$index = $index + 2;
}
}
return $canvas;
}
}
?>