summaryrefslogtreecommitdiff
path: root/web/Processor.php
blob: b1bf0bff0153b87f90fe165cdd64e70ecc8aefef (plain)
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
<?php
/**
 * The Processor class processes an image and prepares
 * the color values needed for the ascii output.
 *
 * Its main job is to process each pixel, do color calculations and
 * store an internal color array for later ascii output, which then is done
 * by its helper Result classes.
 *
 * (c) 2016 Alex Schenkel
 */
namespace Img2Ascii;

class Processor {
    protected $imgPath;
    protected $image;
    protected $imageWidth;
    protected $imageHeight;

    protected $result;

    public function __construct($pathOrImgres) {
        if (is_file($pathOrImgres)) {
            $this->imgPath = $pathOrImgres;
            $this->image = imagecreatefromstring(file_get_contents($pathOrImgres));
        } else {
            $this->image = $pathOrImgres;
        }
        try {
            $this->imageWidth = imagesx($this->image);
            $this->imageHeight = imagesy($this->image);
        } catch (\Exception $e) {
            throw new \Exception("Image creation failed. Is the given resource a valid image?");
        }
    }

    public function asciify($pixelWidth = 10) {
        $charsX = floor($this->imageWidth / $pixelWidth);
        return $this->asciifyToWidth($charsX);
    }

    public function asciifyToWidth($charsX) {
        $charsX = (int)$charsX;
        if (!$charsX) $charsX = floor($this->imageWidth / 10);
        $this->result = null;

        $blockWidth = $this->imageWidth / (float)$charsX;
        $blockHeight = $blockWidth * 8.0/5.0;
        $blockWidth = max(1,floor($blockWidth));
        $blockHeight = max(1,floor($blockHeight));

        $ascii = array();

        for ($y = 0; $y < $this->imageHeight; $y += $blockHeight) {
            for ($x = 0; $x < $this->imageWidth; $x += $blockWidth) {
                $value = [0,0,0];
                if ($x === 0) {
                    $ascii[$y / $blockHeight] = array();
                }
                $counter = 0;
                for ($innerY = $y; $innerY < $y + $blockHeight; $innerY++) {
                    if ($innerY > $this->imageHeight-1) break;
                    for ($innerX = $x; $innerX < $x + $blockWidth; $innerX++) {
                        if ($innerX > $this->imageWidth-1) break;
                        $counter++;
                        $rgb = imagecolorat($this->image,$innerX, $innerY);
                        $rgb = array($rgb >> 16, $rgb >> 8 & 255, $rgb & 255);
                        $value[0] += $rgb[0];
                        $value[1] += $rgb[1];
                        $value[2] += $rgb[2];
                    }
                }
                $value[0] = $value[0] / $counter;
                $value[1] = $value[1] / $counter;
                $value[2] = $value[2] / $counter;
                $ascii[$y / $blockHeight][$x / $blockWidth] = $value;
            }
        }
        $this->result = $ascii;
        return $this;
    }

    protected function saturate($ascii) {
        foreach($ascii as $y =>$line) {
            foreach($line as $x => $value) {
                $ascii[$y][$x] = $value[0]*1 + $value[1]*1 + $value[2]*1;
            }
        }
        return $ascii;
    }

    public function colorResult($blockChar = '#') {
        $res = new ColorResult($this->result);
        $res->blockChar = $blockChar;
        return $res;
    }

    public function result($symbols = "@%#*+=-:. ") {
        $res = new BlackWhiteResult($this->saturate($this->result));
        $res->symbols = $symbols;
        return $res;
    }
}