-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e1bb26
Showing
5 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# ASCII ART GENERATOR | ||
|
||
## Introduction | ||
|
||
This class can generate a text that looks like a given image. | ||
|
||
It traverses the pixels of a given image and converts the color values of each region to a character text, so when you look at the sequence of converted characters it looks like the original image. | ||
|
||
The result text can optionally be outputted formatted as HTML and be displayed in color. | ||
|
||
## Sample output | ||
|
||
### Original image | ||
![Original](http://php.webtutor.pl/ascii-art/example.jpg) | ||
|
||
### ASCII art after image crop | ||
![Result](http://php.webtutor.pl/ascii-art/ascii_art.jpg) | ||
|
||
## Code example | ||
|
||
```php | ||
<?php | ||
|
||
use Tigra\AsciiArt\Generator; | ||
|
||
require_once('generator.php'); | ||
$x = new Generator('example.jpg'); | ||
$color = $x->getBackgroundColor()->getHexValue(); | ||
?> | ||
<html> | ||
<head> | ||
<title>ASCII art demo</title> | ||
</head> | ||
<body bgcolor="#<?php echo $color; ?>"> | ||
<?php | ||
$x->setFontSize(6); | ||
$x->show(range("a", "z"), 0.25, true, true); | ||
?> | ||
</body> | ||
</html> | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "tigra-image-library/ascii-art", | ||
"description": "This class can generate a text that looks like a given image. It traverses the pixels of a given image and converts the color values of each region to a character text, so when you look at the sequence of converted characters it looks like the original image. The result text can optionally be outputted formatted as HTML and be displayed in color.", | ||
"type": "library", | ||
"license": "LGPL-3.0-only", | ||
"authors": [ | ||
{ | ||
"name": "Artur Graniszewski", | ||
"email": "artur.graniszewski@gmail.com" | ||
} | ||
], | ||
"support": { | ||
"email": "artur.graniszewski@gmail.com", | ||
"source": "https://github.com/artur-graniszewski/tigra-ascii-art", | ||
"issues": "https://github.com/artur-graniszewski/tigra-ascii-art/issues" | ||
}, | ||
"homepage": "https://github.com/artur-graniszewski/tigra-ascii-art", | ||
"keywords": [ | ||
"imaging", | ||
"ascii-art", | ||
"gd2", | ||
"php" | ||
], | ||
"require": { | ||
"php": "^5.4", | ||
"ext-gd2": "*", | ||
"tigra-image-library/gd2-imaging": "*" | ||
}, | ||
"require-dev": { | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Tigra\\AsciiArt\\": "." | ||
} | ||
}, | ||
"autoload-dev": { | ||
}, | ||
"scripts": { | ||
"check": [ | ||
"@cs-check", | ||
"@test" | ||
], | ||
"cs-check": "phpcs", | ||
"cs-fix": "phpcbf", | ||
"test": "phpunit" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Tigra\AsciiArt\Generator; | ||
|
||
require_once('generator.php'); | ||
$x = new Generator('example.jpg'); | ||
$color = $x->getBackgroundColor()->getHexValue(); | ||
?> | ||
<html> | ||
<head> | ||
<title>ASCII art demo</title> | ||
</head> | ||
<body bgcolor="#<?php echo $color; ?>"> | ||
<?php | ||
$x->setFontSize(6); | ||
$x->show(range("a", "z"), 0.25, true, true); | ||
?> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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('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; | ||
} | ||
} |