From 6442494822d12c23cdd609031c4039d3309b64f6 Mon Sep 17 00:00:00 2001 From: Cristei Gabriel Date: Mon, 18 Dec 2023 03:19:59 +0200 Subject: dynamic ascii art --- web/Processor.php | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 web/Processor.php (limited to 'web/Processor.php') diff --git a/web/Processor.php b/web/Processor.php new file mode 100644 index 0000000..b1bf0bf --- /dev/null +++ b/web/Processor.php @@ -0,0 +1,103 @@ +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; + } +} -- cgit v1.2.3