Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ Returns the cape of the given user.
| -------------- | --------- | ----------------------- |
| `access_token` | string | The user's access token |
| `cape` | image/png | The cape file |

### Json

**GET** `/api/skin-api/textures/{user_name}`
Return the json like this
```
{
"SKIN": {
"url": "http://example.com/api/skin-api/skins/gfortes",
"digest": "SHA256 HASH (HEX)",
"metadata": {
"model": "slim"
}
},
"CAPE": {
"url": "http://example.com/api/skin-api/capes/gfortes",
"digest": "SHA256 HASH (HEX)"
}
}
```
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Azuriom\Plugin\SkinApi\Controllers\Api\ApiController;
use Azuriom\Plugin\SkinApi\Controllers\TextureJsonController;
use Illuminate\Support\Facades\Route;

// Skins
Expand All @@ -17,3 +18,6 @@
Route::get('/capes/{user}', [ApiController::class, 'cape']);
Route::post('/capes', [ApiController::class, 'updateCape'])->name('capes.update');
Route::delete('/capes', [ApiController::class, 'deleteCape'])->name('capes.delete');

// Json Provider
Route::get('/textures/{username}', [TextureJsonController::class, 'handle']);
72 changes: 72 additions & 0 deletions src/Controllers/TextureJsonController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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';
}
}