-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.php
executable file
·211 lines (192 loc) · 5.24 KB
/
generator.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
<?php
namespace Tigra\AsciiArt;
use Tigra\Color;
use Tigra\Dimensions;
use Tigra\Image;
use Tigra\Point;
use Tigra\Quantizator;
/**
* GD2 Imaging (part of Lotos Framework)
*
* Copyright (c) 2005-2011 Artur Graniszewski (aargoth@boo.pl)
* All rights reserved.
*
* @category Library
* @package Lotos
* @subpackage Imaging
* @copyright Copyright (c) 2005-2011 Artur Graniszewski (aargoth@boo.pl)
* @license GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* @version 1.0.0
*/
/**
* ASCII art generator class.
*/
class Generator
{
/**
* Image to convert
*
* @var Image
*/
protected $image;
/**
* Color threshold.
*
* The higher the threshold, the smaller HTML document (and worse image accuracy).
* @var int
*/
protected $threshold = 4;
/**
* Background color.
*
* @var Color
*/
protected $backgroundColor = 1;
/**
* Image CSS style in HTML mode
*
* @var mixed
*/
protected $css = 'font-family: monospace; font-size: 12px;';
/**
* Creates ASCII art generator.
*
* @param string $fileName Image file name.
* @return Generator
*/
public function __construct($fileName) {
if(!ini_get('safe_mode') && strpos(ini_get('disabled_functions'), 'set_time_limit') === false) {
set_time_limit(0);
}
$this->image = new Image($fileName);
$this->backgroundColor = $this->image->getBackgroundColor();
}
/**
* Sets the font size.
*
* @param int $size Font size in pixels.
* @return Generator
*/
public function setFontSize($size) {
$this->css = 'font-family: monospace; font-size: '.(int) $size.'px;';
return $this;
}
/**
* Sets the color threshold.
*
* The higher the threshold, the smaller HTML document (and worse image accuracy).
*
* @param int $value
* @return Generator
*/
public function setColorThreshold($value) {
$this->threshold = $value;
return $this;
}
/**
* Returns background color
*
* @return Color
*/
public function getBackgroundColor() {
return $this->backgroundColor;
}
/**
* Displays ASCII art.
*
* @param string[] $charsList List of characters to use.
* @param float $resizeFactor Image resize factor, for example: 2.0 = image resized to 50% of its orignal size, 0.5 = image resized by 100%
* @param bool $useHtml Use HTML entities? Default: true
* @param bool $useColor Use HTML colors? Default: true
* @return Generator
*/
public function show($charsList, $resizeFactor = 1, $useHtml = true, $useColor = true) {
if($useColor) {
$useHtml = true;
}
$this->image->resize(($this->image->getWidth() - ($this->image->getWidth() % 8)) / $resizeFactor, ($this->image->getHeight() - ($this->image->getHeight() % 8)) / $resizeFactor);
$colorImage = $this->image->getCopy();
$charsImage = new Image(__DIR__ . '/ascii_art.png');
$width = $this->image->getWidth();
$height = $this->image->getHeight();
$allChars = implode('', range("a", "z")).implode('', range('A', 'Z')).implode('', range(0, 9)).'.,;!@#$%^&*()-= +[]\\{}:"<>?';
$allChars = str_split($allChars, 1);
foreach($allChars as $index => $char) {
if(!in_array($char, $charsList)) {
$allChars[$index] = null;
}
}
$quantizator = new Quantizator();
$charSize = new Dimensions(8, 11);
foreach($allChars as $index => $char) {
if(!$char) {
continue;
}
// fetch an image of the given character
$charImage = $charsImage->getSubImage(new Point($index * 8, 0), $charSize);
// add vector to the glyphs collection
$quantizator->addGlyph($charImage->getVector(), $char);
}
$this->image->toGrayScale();
$this->image->toBinary(false, true);
$gd = $colorImage->getGd2Handle();
if($useHtml) {
echo '<div style="'.$this->css.'">';
}
$lastColor = -1;
$r1 = -1000; $g1 = 0; $b1 = 0;
for($y = 0; $y < $height; $y += 11) {
for($x = 0; $x < $width; $x += 8) {
$search = $this->image->getSubImage(new Point($x, $y), $charSize)->getVector();
$result = $quantizator->findNearestEuklid($search);
$char = $result[0];
if($useHtml) {
$char = htmlspecialchars($char);
}
if($useColor) {
$r = 0; $g = 0; $b = 0;
$hits = 0;
for($k = $x, $maxX = min($x + 8, $width); $k < $maxX; ++$k) {
for($l = $y, $maxY = min($y + 11, $height); $l < $maxY; ++$l) {
$rgb = imagecolorat($gd, $k, $l);
$r += ($rgb >> 16) & 0xff;
$g += ($rgb >> 8) & 0xff;
$b += $rgb & 0xff;
$hits++;
}
}
$r /= $hits;
$g /= $hits;
$b /= $hits;
$color = new Color($r, $g, $b);
$color = $color->getHexValue();
if($lastColor !== $color && (abs($r - $r1) > $this->threshold || abs($g - $g1) > $this->threshold || abs($b - $b1) > $this->threshold)) {
if($lastColor !== -1) {
$lastColor = $color;
echo '</span>';
} else {
}
$lastColor = $color;
$r1 = $r;
$g1 = $g;
$b1 = $b;
echo '<span style="color: #'.$color.'">';
}
}
echo $char;
}
if($useHtml) {
echo "<br />";
} else {
echo "\n";
}
}
if($useColor) {
echo '</span>';
}
if($useHtml) {
echo "</div>";
}
return $this;
}
}