-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSkinAPI.php
More file actions
191 lines (164 loc) · 5.58 KB
/
SkinAPI.php
File metadata and controls
191 lines (164 loc) · 5.58 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Azuriom\Plugin\SkinApi;
use Azuriom\Plugin\SkinApi\Render\RenderType;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
class SkinAPI
{
public static function getConstraints(bool $cape = false): array
{
$prefix = $cape ? 'skin.capes.' : 'skin.';
$width = (int) setting($prefix.'width', 64);
$height = (int) setting($prefix.'height', 64);
$scale = (int) setting($prefix.'scale', 1);
if ($scale === 1) {
return ['width' => $width, 'height' => $height];
}
return [
'min_width' => $width,
'min_height' => $height,
'max_width' => $width * $scale,
'max_height' => $height * $scale,
];
}
public static function getRule(bool $cape = false): string
{
return Rule::dimensions(static::getConstraints($cape));
}
public static function defaultSkin(): ?string
{
if (! Storage::disk('public')->exists('skins/default.png')) {
return null;
}
return Storage::disk('public')->url('skins/default.png');
}
public static function skinUrl(int $userId, bool $cape = false): ?string
{
$path = $cape ? 'skins/capes/' : 'skins/';
if (! Storage::disk('public')->exists($path.$userId.'.png')) {
return null;
}
$lastModified = Storage::disk('public')->lastModified($path.$userId.'.png');
$hash = $lastModified ? '?h='.substr($lastModified, 4) : '';
return url(Storage::disk('public')->url($path.$userId.'.png'.$hash));
}
/**
* Code from https://github.com/scholtzm/php-minecraft-avatars
*
* With modification for HD Skins by Gru and DeepSeek :)
*
* @license MIT
* @author Michael Scholtz
*/
public static function makeAvatarWithTypeForUser(RenderType $type, string $user): void
{
abort_unless(extension_loaded('gd'), 403, 'Please enable the GD extension in your php.ini');
$skinPath = Storage::disk('public')->path("skins/{$user}.png");
$skin = imagecreatefrompng($skinPath);
$skinWidth = imagesx($skin);
$skinHeight = imagesy($skin);
$scale = $skinWidth / 64;
$size = 64;
$x = 46;
$y = 30;
$image = imagecreatetruecolor($size, $size);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
// function of scaling params
$s = function ($value) use ($scale) {
return $value * $scale;
};
// Background
// Face layer
imagecopyresampled(
$image, $skin,
0, 0,
$s(8), $s(8),
$size, $size,
$s(8), $s(8)
);
// Second layer
imagecopyresampled(
$image, $skin,
0, 0,
$s(40), $s(8),
$size, $size,
$s(8), $s(8)
);
if ($type === RenderType::COMBO) {
$head = imagecreate(10, 10);
$white = imagecolorallocate($head, 255, 255, 255);
imagecopyresampled($image, $head, $x + 3, $y - 1, 0, 0, 10, 10, 10, 10);
imagecolordeallocate($head, $white);
imagedestroy($head);
$torso = imagecreate(18, 14);
$white = imagecolorallocate($torso, 255, 255, 255);
imagecopyresampled($image, $torso, $x - 1, $y + 7, 0, 0, 18, 14, 18, 14);
imagecolordeallocate($torso, $white);
imagedestroy($torso);
$legs = imagecreate(10, 14);
$white = imagecolorallocate($legs, 255, 255, 255);
imagecopyresampled($image, $legs, $x + 3, $y + 19, 0, 0, 10, 14, 10, 14);
imagecolordeallocate($legs, $white);
imagedestroy($legs);
// white shadow - end
// Foreground
// Face
imagecopyresampled(
$image, $skin,
$x + 4, $y,
$s(8), $s(8),
8, 8,
$s(8), $s(8)
);
// Body
imagecopyresampled(
$image, $skin,
$x + 4, $y + 8,
$s(20), $s(20),
8, 12,
$s(8), $s(12)
);
// Left arm
imagecopyresampled(
$image, $skin,
$x, $y + 8,
$s(44), $s(20),
4, 12,
$s(4), $s(12)
);
// Right arm (flipped)
imagecopyresampled(
$image, $skin,
$x + 12, $y + 8,
$s(47), $s(20),
4, 12,
-$s(4), $s(12)
);
// Left leg
imagecopyresampled(
$image, $skin,
$x + 4, $y + 20,
$s(4), $s(20),
4, 12,
$s(4), $s(12)
);
// Right leg (flipped)
imagecopyresampled(
$image, $skin,
$x + 8, $y + 20,
$s(7), $s(20),
4, 12,
-$s(4), $s(12)
);
}
if (! file_exists($dir_path = Storage::disk('public')->path($type->value))) {
mkdir($dir_path, 0755, true);
}
imagepng($image, Storage::disk('public')->path("{$type->value}/{$user}.png"));
// release resources
imagedestroy($skin);
imagedestroy($image);
}
}