-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.php
More file actions
127 lines (110 loc) · 5.4 KB
/
index.php
File metadata and controls
127 lines (110 loc) · 5.4 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
<?php
require_once __DIR__ ."/../vendor/autoload.php";
use TdTrung\Chalk\Chalk;
use Treinetic\ImageArtist\lib\Image;
const OUTPUT_DIR = __DIR__ ."/../images_processed";
$chalk = new Chalk();
// Greetz the user
echo $chalk->color69(base64_decode("LmRQIlk4ICAgIGRiICAgIDhiICAgIGQ4IDg4IiJZYiA4OCAgICAgODg4ODg4ICAgICA4OGIgODggODg4ODg4IDg4ODg4OCAgICAgODgiIlliIDg4IiJZYiAgZFAiWWIgICA4ODg4OCA4ODg4ODggIGRQIiJiOCA4ODg4ODggCmBZYm8uIiAgIGRQWWIgICA4OGIgIGQ4OCA4OF9fZFAgODggICAgIDg4X18gICAgICAgODhZYjg4IDg4X18gICAgIDg4ICAgICAgIDg4X19kUCA4OF9fZFAgZFAgICBZYiAgICAgODggODhfXyAgIGRQICAgYCIgICA4OCAgIApvLmBZOGIgIGRQX19ZYiAgODhZYmRQODggODgiIiIgIDg4ICAubyA4OCIiICAgICAgIDg4IFk4OCA4OCIiICAgICA4OCAgICAgICA4OCIiIiAgODgiWWIgIFliICAgZFAgby4gIDg4IDg4IiIgICBZYiAgICAgICAgODggICAKOGJvZFAnIGRQIiIiIlliIDg4IFlZIDg4IDg4ICAgICA4OG9vZDggODg4ODg4ICAgICA4OCAgWTggODggICAgICAgODggICAgICAgODggICAgIDg4ICBZYiAgWWJvZFAgICJib2RQJyA4ODg4ODggIFlib29kUCAgIDg4ICAg")) . PHP_EOL . PHP_EOL;
// Ask for the collection name
$defaultCollectionName = "Sample NFT Project";
echo $chalk->blue("What do you want to name this collection?", $chalk->yellow(implode("", ["[", $defaultCollectionName, "]"])));
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$inputCollectionName = strlen(trim($line)) > 0 ? trim($line) : $defaultCollectionName;
// Ask where the collection data is held
$defaultCollectionEndpoint = "https://harrydenley.com/assets/nft/samplenftproject/";
echo $chalk->blue("Where are you storing the metadata?", $chalk->yellow(implode("", ["[", $defaultCollectionEndpoint, "]"])));
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$defaultCollectionEndpoint = strlen(trim($line)) > 0 ? trim($line) : $defaultCollectionEndpoint;
// Do some directory hygiene
if(count(scandir(OUTPUT_DIR)) > 0) {
echo PHP_EOL . $chalk->lightRed("[!] Output directory is not empty.", PHP_EOL, $chalk->white("Do you want to delete everything in here beforehand?\r\n If no, application will NOT overwrite files with the same file names", $chalk->lightYellow("[no]")));
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$input = strtolower(trim($line));
if(in_array($input, ["y", "yes"])) {
$files = glob(OUTPUT_DIR ."/*");
foreach($files as $file){
if(is_file($file)) {
unlink($file);
}
}
}
}
// Fetch all the layers and detail everything is correct
echo PHP_EOL . $chalk->color10("[*] Fetching and parsing layers") . PHP_EOL;
$files = glob(__DIR__ ."/../images_raw/*/*.png");
$table = [];
foreach($files as $file) {
$filename = basename($file);
list($trait_name, $trait_value1, $trait_value2) = preg_split("/\_|\./", $filename);
$table[$trait_name][] = [
"trait_shape" => $trait_value1,
"trait_color" => $trait_value2,
"_filename" => $filename,
"_abs_path" => $file
];
}
// Creating the images
echo PHP_EOL . $chalk->color10("[*] Creating images") . PHP_EOL;
$defaultIterations = 1;
echo $chalk->blue("How many random iterations do you want?", $chalk->yellow(implode("", ["[", $defaultIterations, "]"])));
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$input = (trim($line) > 0 ? trim($line) : $defaultIterations) + 1;
$fileIndex = 0;
for($i=1;$i<=$input;$i++) {
foreach($table["head"] as $key => $headLayer) {
$attr = [];
$head = new Image($headLayer["_abs_path"]);
$attr[] = [
"trait_type" => "Head Color",
"value" => $headLayer["trait_color"]
];
$attr[] = [
"trait_type" => "Head Shape",
"value" => $headLayer["trait_shape"]
];
$random_eye_index = mt_rand(0, count($table["eyes"]) - 1);
$eyeLayer = $table["eyes"][$random_eye_index];
$eyes = new Image($eyeLayer["_abs_path"]);
$head->merge($eyes, 0, 0);
$attr[] = [
"trait_type" => "Eye Color",
"value" => $eyeLayer["trait_color"]
];
$attr[] = [
"trait_type" => "Eye Shape",
"value" => $eyeLayer["trait_shape"]
];
$random_mouth_index = mt_rand(0, count($table["mouth"]) - 1);
$mouthLayer = $table["mouth"][$random_mouth_index];
$mouth = new Image($mouthLayer["_abs_path"]);
$head->merge($mouth, 0, 0);
$attr[] = [
"trait_type" => "Mouth Color",
"value" => $mouthLayer["trait_color"]
];
$attr[] = [
"trait_type" => "Mouth Shape",
"value" => $mouthLayer["trait_shape"]
];
if(file_exists(implode("/", [OUTPUT_DIR, $fileIndex.".png"])) === false) {
$head->save(implode("/", [OUTPUT_DIR, $fileIndex.".png"]), IMAGETYPE_PNG);
echo PHP_EOL . $chalk->color77("\t[+]Image ${fileIndex} has been created!", PHP_EOL, $chalk->white("\t".json_encode($attr))) . PHP_EOL;
// Build the trait file for the graphic
$traits = [
"attributes" => $attr,
"descriptions" => "",
"image" => implode("", [$defaultCollectionEndpoint, $fileIndex, ".png"]),
"name" => implode(" ", [$inputCollectionName, "#". $fileIndex])
];
file_put_contents(implode("/", [OUTPUT_DIR, $fileIndex]), json_encode($traits));
} else {
echo PHP_EOL . $chalk->color74("\t[+]Image ${fileIndex} already exists - not overwritten!", PHP_EOL);
}
$fileIndex++;
}
}