-
Notifications
You must be signed in to change notification settings - Fork 0
/
Img2Ascii.php
217 lines (178 loc) · 5.39 KB
/
Img2Ascii.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
class Img2Ascii
{
protected string $imageFile;
protected GdImage $gdImage;
protected array $imageInfo;
//protected string $chars = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~i!lI;:,^`';
protected string $chars = '@#$%?*+;:,.';
protected int $blockSize = 1;
protected bool $inverse = false;
protected bool $multibyte = false;
public function __construct(string $imageFile = null)
{
if (isset($imageFile))
{
$this->setImageFile($imageFile);
}
}
public function setImageFile(string $imageFile): void
{
if (!file_exists($imageFile))
{
throw new RuntimeException("Image file does not exist: " . $imageFile);
}
$this->imageFile = $imageFile;
}
public function setBlockSize(int $blockSize): void
{
$this->blockSize = $blockSize;
}
public function inverse(bool $inverse = true): void
{
$this->inverse = $inverse;
}
public function multibyte(bool $multibyte = true): void
{
$this->multibyte = $multibyte;
}
public function setChars(string $chars, bool $multibyte = false): void
{
$this->chars = $chars;
$this->multibyte = $multibyte;
}
public function write(string $eol = PHP_EOL): void
{
$this->open();
$this->walk(
function ($char)
{
echo $char;
},
function () use ($eol)
{
echo $eol;
});
}
public function get(): string
{
$result = "";
$this->open();
$this->walk(
function ($char) use (&$result)
{
$result .= $char;
},
function () use (&$result)
{
$result .= PHP_EOL;
});
return $result;
}
public function toStream($stream): void
{
$this->open();
$this->walk(
function ($char) use ($stream)
{
fwrite($stream, $char);
},
function () use ($stream)
{
fwrite($stream, PHP_EOL);
});
}
public function callback(callable $singleChar, callable $newLine): void
{
$this->open();
$this->walk($singleChar(...), $newLine(...));
}
protected function open(): void
{
$info = getimagesize($this->imageFile);
if (false === $info)
{
throw new RuntimeException("Wrong image file: " . $this->imageFile);
}
$img = match ($info["mime"])
{
"image/jpeg" => \imagecreatefromjpeg($this->imageFile),
"image/png" => \imagecreatefrompng($this->imageFile),
"image/gif" => \imagecreatefromgif($this->imageFile),
default => throw new RuntimeException("Unsupported image type: " . $info["mime"])
};
if (false === $img)
{
throw new RuntimeException("Wrong image file: " . $this->imageFile);
}
$this->imageInfo = $info;
$this->gdImage = $img;
}
protected function walk(callable $singleChar, callable $newLine): void
{
for ($y = 0; $y < $this->imageInfo[1]; $y += $this->blockSize)
{
for ($x = 0; $x < $this->imageInfo[0]; $x += $this->blockSize)
{
$block = $this->readBlock($x, $y);
$char = $this->blockToAscii($block);
$singleChar($char);
}
$newLine();
}
}
protected function blockToAscii(array $block): string
{
$maxBrightness = count($block) * count($block[0]);
$sumBrightness = 0;
$len = $this->multibyte ? mb_strlen($this->chars) : strlen($this->chars);
foreach ($block as $row)
{
foreach ($row as $pixel)
{
// 0..1, from dark to bright
$brightness = ($pixel["r"] + $pixel["g"] + $pixel["b"]) / (3 * 255);
// 0..1, from transparent to opaque
$alphaRatio = $pixel["a"] / 128;
$alphaBrightness = (1 - $brightness) * $alphaRatio;
$sumBrightness += $brightness + $alphaBrightness;
}
}
$index = floor($sumBrightness / $maxBrightness * ($len - 1));
if ($this->inverse)
{
$index = -$index;
}
return $this->multibyte
? mb_substr($this->chars, $index, 1)
: substr($this->chars, $index, 1);
}
protected function readBlock(int $x, int $y): array
{
$block = [];
for ($offsetY = 0; $offsetY < $this->blockSize && $y + $offsetY < $this->imageInfo[1]; $offsetY++)
{
$row = [];
for ($offsetX = 0; $offsetX < $this->blockSize && $x + $offsetX < $this->imageInfo[0]; $offsetX++)
{
$row[] = $this->rgbAt($x + $offsetX, $y + $offsetY);
}
$block[] = $row;
}
return $block;
}
protected function rgbAt(int $x, int $y): array
{
$rgb = imagecolorat($this->gdImage, $x, $y);
$a = ($rgb >> 24) & 0x7F;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return [
"r" => $r,
"g" => $g,
"b" => $b,
"a" => $a,
];
}
}