-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe-converter.php
63 lines (51 loc) · 1.67 KB
/
frame-converter.php
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
<?php
use function Co\go;
Swoole\Runtime::enableCoroutine(); //i have swoole installed, so why not to use???
const FRAME_WIDTH = 52;
const FRAME_HEIGHT = 32;
const FRAME_DIR = __DIR__ . "/frames";
const ASCII_FRAME_DIR = __DIR__ . "/frames-ascii";
const MAX_BRIGHTNESS = 255 + 255 + 255;
const SCALE = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`\'.';
$scaleArray = str_split(SCALE);
$scaleLength = count($scaleArray);
function getFrameColor(GdImage $image, int $x, int $y): int
{
$rgb = imagecolorat($image, $x, $y);
$red = ($rgb >> 16) & 255;
$green = ($rgb >> 8) & 255;
$blue = $rgb & 255;
return $red + $green + $blue;
}
function getFrameChar(GdImage $image, int $x, int $y): string
{
global $scaleLength;
$color = getFrameColor($image, $x, $y);
$index = intval((MAX_BRIGHTNESS / ($scaleLength)) * ($color / MAX_BRIGHTNESS));
return SCALE[$scaleLength - $index - 1];
}
function getFrameMap(int $frameNum): string
{
$framePath = FRAME_DIR . "/frame$frameNum.jpeg";
$image = imagecreatefromjpeg($framePath);
$res = "";
for($y = 0; $y < FRAME_HEIGHT; $y++) {
for($x = 0; $x < FRAME_WIDTH; $x++) {
$res .= getFrameChar($image, $x, $y);
}
$res .= "\n";
}
return $res;
}
$dir = new DirectoryIterator(FRAME_DIR);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$name = $fileinfo->getFilename();
$frame = (int)str_replace("frame", "", explode(".jpeg", $name)[0]);
if($frame !== 0) {
go(function () use ($frame) {
file_put_contents(ASCII_FRAME_DIR . "/$frame", getFrameMap($frame));
});
}
}
}