-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImagePrinter.php
87 lines (70 loc) · 2.2 KB
/
ImagePrinter.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
<?php
/*
* This file is part of php-drawille
*
* (c) Jeff Welch <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\Point;
use Drawille\Canvas;
use Fab\SuperFab;
class ImagePrinter
{
private $image;
private $threshold;
private $ratio;
private $invert;
private $fab;
public function __construct($image, $threshold = 385.2, $ratio = null, $invert = false, $fab = false) {
$this->image = $image;
$this->threshold = (float) $threshold;
$this->ratio = $ratio;
$this->invert = $invert;
$this->fab = $fab;
}
public function run($terminalWidth, $terminalHeight) {
$imagine = new Imagine();
$image = $imagine->open($this->image);
$size = $image->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
if ($this->ratio) {
$ratio = (float) $this->ratio;
$width = floor($width * $ratio);
$height = floor($height * $ratio);
$image->resize(new Box($width, $height));
}
else {
$height_ratio = $terminalHeight * 4 / $height;
$width_ratio = $terminalWidth * 2 / $width;
$ratio = min($height_ratio, $width_ratio);
if ($ratio < 1.0) {
$width = floor($width * $ratio);
$height = floor($height * $ratio);
$image->resize(new Box($width, $height));
}
}
$canvas = new Canvas();
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$color = $image->getColorAt(new Point($x, $y));
$total = $color->getRed() + $color->getGreen() + $color->getBlue();
if (!$this->invert ^ $total > $this->threshold) {
$canvas->set($x, $y);
}
}
}
if ($this->fab) {
$fab = new SuperFab();
echo $fab->paint($canvas->frame()), "\n";
}
else {
echo $canvas->frame(), "\n";
}
}
}
?>