-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTextureJsonController.php
More file actions
72 lines (57 loc) · 2.02 KB
/
TextureJsonController.php
File metadata and controls
72 lines (57 loc) · 2.02 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
<?php
namespace Azuriom\Plugin\SkinApi\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Azuriom\Models\User;
class TextureJsonController extends Controller
{
public function handle(string $username)
{
$user = User::where('name', $username)->first();
$result = [];
$userId = $user ? $user->id : null;
//SKIN
if ($userId && Storage::disk('public')->exists("skins/{$userId}.png")) {
$skinFile = "skins/{$userId}.png";
} elseif (Storage::disk('public')->exists("skins/default.png")) {
$skinFile = "skins/default.png";
} else {
$skinFile = null;
}
if ($skinFile) {
$skinPath = Storage::disk('public')->path($skinFile);
$result['SKIN'] = [
'url' => url("/api/skin-api/skins/$username"),
'digest' => hash_file('sha256', $skinPath),
'metadata' => [
'model' => $this->detectModel($skinPath)
]
];
}
//CAPE
if ($userId && Storage::disk('public')->exists("capes/{$userId}.png")) {
$capeFile = "capes/{$userId}.png";
} elseif (Storage::disk('public')->exists("capes/default.png")) {
$capeFile = "capes/default.png";
} else {
$capeFile = null;
}
if ($capeFile) {
$capePath = Storage::disk('public')->path($capeFile);
$result['CAPE'] = [
'url' => url("/api/skin-api/capes/$username"),
'digest' => hash_file('sha256', $capePath)
];
}
return response()->json($result, 200, [], JSON_UNESCAPED_SLASHES);
}
private function detectModel(string $path): string
{
$img = imagecreatefrompng($path);
$rgb = imagecolorat($img,55,20);
$colors = imagecolorsforindex($img, $rgb);
$alpha = $colors["alpha"];
imagedestroy($img);
return $alpha === 127 ? 'slim' : 'default';
}
}