summaryrefslogtreecommitdiff
path: root/includes/assets.inc
blob: 3f9971a05ed599435b8139ad332ac35c1fcbc59f (plain)
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
<?php

if (!file_exists("../assets/avatars")) mkdir("../assets/avatars");
if (!file_exists("../assets/banners")) mkdir("../assets/banners");
if (!file_exists("../assets/heads")) mkdir("../assets/heads");
if (!file_exists("../assets/bodies")) mkdir("../assets/bodies");

function downloadAssets($system) {
    $general = json_decode(file_get_contents("./data/$system/general.json"), true);

    if (isset($general["avatar_url"])) {
        $id = preg_replace("/^([\da-f]{8})-([\da-f]{4})-([\da-f]{4})-([\da-f]{4})-([\da-f]{12})$/", "$1$2$3$4$5", $general["uuid"]);
        echo("    /avatars/$id.webp\n");
        file_put_contents("/tmp/img." . pathinfo($general['avatar_url'], PATHINFO_EXTENSION), file_get_contents($general['avatar_url']));
        exec("convert -resize 512x512 \"" . "/tmp/img." . pathinfo($general['avatar_url'], PATHINFO_EXTENSION) . "\" ../assets/avatars/" . $id . ".webp");
        exec("convert -resize 512x512 \"" . "/tmp/img." . pathinfo($general['avatar_url'], PATHINFO_EXTENSION) . "\" ../assets/uploads/" . ($id === "gdapd" ? "raindrops" : "cloudburst") . ".png");
        unlink("/tmp/img." . pathinfo($general['avatar_url'], PATHINFO_EXTENSION));
    }

    if (isset($general["banner"])) {
        $id = preg_replace("/^([\da-f]{8})-([\da-f]{4})-([\da-f]{4})-([\da-f]{4})-([\da-f]{12})$/", "$1$2$3$4$5", $general["uuid"]);
        echo("    /banners/$id.webp\n");
        file_put_contents("/tmp/img." . pathinfo($general['banner'], PATHINFO_EXTENSION), file_get_contents($general['banner']));
        exec("convert -resize 2048x2048 \"" . "/tmp/img." . pathinfo($general['banner'], PATHINFO_EXTENSION) ."\" ../assets/banners/" . $id . ".webp");
        unlink("/tmp/img." . pathinfo($general['banner'], PATHINFO_EXTENSION));
    }

    $members = json_decode(file_get_contents("./data/$system/members.json"), true);

    foreach ($members as $member) {
        $id = preg_replace("/^([\da-f]{8})-([\da-f]{4})-([\da-f]{4})-([\da-f]{4})-([\da-f]{12})$/", "$1$2$3$4$5", $general["uuid"]) . preg_replace("/^([\da-f]{8})-([\da-f]{4})-([\da-f]{4})-([\da-f]{4})-([\da-f]{12})$/", "$1$2$3$4$5", $member["uuid"]);

        if (isset($member["avatar_url"])) {
            echo("    /avatars/$id.webp\n");
            file_put_contents("/tmp/img." . pathinfo($member['avatar_url'], PATHINFO_EXTENSION), file_get_contents($member['avatar_url']));
            exec("convert -resize 512x512 \"" . "/tmp/img." . pathinfo($member['avatar_url'], PATHINFO_EXTENSION) . "\" ../assets/avatars/" . $id . ".webp");
            unlink("/tmp/img." . pathinfo($member['avatar_url'], PATHINFO_EXTENSION));
        }

        echo("    /banners/$id.webp\n");

        if (isset($member["banner"])) {
            file_put_contents("/tmp/img." . pathinfo($member['banner'], PATHINFO_EXTENSION), file_get_contents($member['banner']));
            exec("convert -resize 2048x2048 \"" . "/tmp/img." . pathinfo($member['banner'], PATHINFO_EXTENSION) . "\" ../assets/banners/" . $id . ".webp");
            unlink("/tmp/img." . pathinfo($member['banner'], PATHINFO_EXTENSION));
        } else {
            $img = imagecreate(2048, 1024);

            if (isset($member["color"])) {
                imagecolorallocate($img, hexdec(substr($member["color"], 0, 2)) / 2, hexdec(substr($member["color"], 2, 2)) / 2, hexdec(substr($member["color"], 4, 2)) / 2);
            } else {
                imagecolorallocate($img, 0, 0, 0);
            }

            imagejpeg($img, "/tmp/img.jpeg", 100);
            imagedestroy($img);
            exec("convert -resize 2048x2048 \"/tmp/img.jpeg\" ../assets/banners/" . $id . ".webp");
            unlink("/tmp/img.jpeg");
        }

        if (file_exists("../assets/uploads/pt-" . $member["name"] . ".png")) {
            $url = "../assets/uploads/pt-" . $member["name"] . ".png";
        } else {
            $url = "../assets/uploads/pt.png";
        }
        echo("    /heads/$id.webp\n");
        exec("convert -resize 64x64 \"" . $url . "\" ../assets/heads/" . $id . ".webp");

        if (file_exists("../app/fronters/ponies/" . $member["id"] . ".png")) {
            echo("    /bodies/$id.webp\n");
            exec("convert -resize 128x128 \"" . "../app/fronters/ponies/" . $member["id"] . ".png" . "\" ../assets/bodies/" . $id . ".webp");
        }
    }
}

downloadAssets("gdapd");
downloadAssets("ynmuc");