Luminova Framework

PHP Luminova: Image to ASCII Art Generator

Last updated: 2026-11-28 20:57:12

Convert JPEG, PNG, GIF, and WebP images into ASCII art for the terminal with custom character sets, colors, and dimensions.

The Luminova\Command\Utils\Image class uses PHP's GD extension to load images and map pixel brightness to characters. Choose a character set to control the shading, add foreground colors, and set the output dimensions to fit your terminal. Try weighted grayscale for images with text or fine details.

Usage Examples

Basic Image to ASCII Conversion

Convert an image to ASCII art using the inverted ASCII character set.

use Luminova\Command\Utils\Image;

$image = Image::draw(
    root('/public/assets/images/') . 'photo.png',
    Image::ASCII_INVERTED
);

echo $image;

Custom ASCII Characters and Color Mapping

In this example, we specify a custom ASCII character set and map each character to a specific color, enhancing the visual effect.

use Luminova\Command\Utils\Image;

$ascii = '+#0 ';
$image = Image::draw(
    root('/public/assets/images/') . 'logo.png',
    $ascii,
    colors: Image::colors($ascii, ['blue', 'red', 'white', 'green'])
);

echo $image;

Resizing and Detailed Customization

Set a target width, enable weighted grayscale, and print each row with a delay. Omitting the height preserves the aspect ratio.

use Luminova\Command\Utils\Image;

Image::draw(
    root('/public/assets/images/') . 'photo.png',
    ascii: Image::ASCII_CLASSIC,
    width: 80,
    grayscale: true,
    colors: ['red', 'blue', 'yellow', 'magenta', 'cyan'],
    lazyPrint: 100_000
);

The following sample illustrates the shading produced by a denser character set.

%%%%%%%%%%%%%%%%%%%%%%%%%%#+-:::::-+*#######################
%%%%%%%%%%%%%%%%%%%%%%*=-:.          -######################
%%%%%%%%%%%%%%%%%%%%#-  ..           :######################
%%%%%%%%%%%%%%%%%%%%+   ..::-===:.   :######################
%%%%%%%%%%%%%%%%%%%%+   :-===+++=-:. .*#####################
%%%%%%%%%%%%%%%%%%%%#.  --=+++**+=-: .*#####################
%%%%%%%%%%%%%%%%%%%#: ..:-===---+==-.-######################
%%%%%%%%####%%%%%%%+   .-+**+++=++=- *######################
%%%%%%#########%%%%+  ..:=+==++--==..:*#####################
%%%%%############%#+   ..-===++=--..  .*############********
%%%################*    ----===--:..   :###########*********
##%%##############**:::-=-=++*++=--=---=*+*****##***********
################+=+**=----++**+++==--=**+=-=+***************
##############*-:---=+*+==+++**+*++*#+*=:-:::=**************
#############+:::--:-==*#**##****#*#++-:::::::-*####********
*******#####*=+=::--::--=**###%%##*=--:--:--=-==#####*******
*********##*==+===::-:-----**%%%%*=--:-::-=*+#**+***********
***********+=**+#+=------+#%#%#*##%#=--:-+**+**+=***********
***********-=+****+#*++=##%%*##*#**%#+-=+=++=-:--+**********
**********+::::-==*#*#*#%#%#*######%%#*+*=::-::--=+****++***
++++******-:::::::-**#*#**=***+***+==+++=+::::---=++++++++++
++++++++++=:::::::::-===####%##%%%%%%%#+=:::--=-=+++++++++++
+++++++++++=-----::::::+%%%%%%%%%%%%%%%-:-..:----+++++++++++
+++++++++++=-::::::-:::%%%%%%%%%%%%%%%%#:::..:----++++++++++
+++++++++++=-::::::...=%%%%%%%%%%%%%%%%%+::. :----++++++++++
++++++++++++=-:::::.::*%%%%%%%%%%%%%%%%%*::..-----++++++++++
+++++++++++++=-------:%%%%%%%%%%%%%%%%%%%-:::=++--++++++++++
+++++++++++++=::------=+*#%#########**+*+::----=+-++++++++++
++++++++++++=:::::---=+==----------::::::---=====-++++++++++

Class Definition

  • Class namespace: Luminova\Command\Utils\Image

Constants

Predefined characters suitable for ASCII art, each constant defines a specific set varying in density or pattern to create distinct visual textures.

ConstantTypeDescription
ASCII_DENSEstringA dense ASCII character set with symbols for detailed shading.
ASCII_EXTENDED_DENSEstringAn extended, dense ASCII set with a wide range of characters for fine-grained texture.
ASCII_SIMPLIFIEDstringA simplified ASCII set offering distinct, high-contrast shading.
ASCII_INVERTEDstringAn inverted ASCII set that transitions from light to dark.
ASCII_BLOCKSstringA block-based ASCII set for bold, heavy shading and block-style textures.
ASCII_CLASSICstringA classic ASCII set with simple symbols for moderate shading.
ASCII_BINARYstringA binary ASCII set for creating binary-based textures or representations.
ASCII_BINARY_CLASSICstringA binary set with a space prefix to add contrast.
ASCII_BINARY_SIMPLIFIEDstringA binary set with spaces for enhanced spacing and readability.

Methods

draw

Converts an image to ASCII art with optional color mapping and delayed terminal output. Supply a color list in character order or a map keyed by character. An integer delay prints one row at a time. An array can specify a pixel delay for each character or a pixels delay for each row. All delays are in microseconds.

public static function draw(
    string $path,
    string $ascii = self::ASCII_CLASSIC,
    ?int $width = null,
    ?int $height = null,
    bool $grayscale = false,
    array $colors = [],
    array|int|null $lazyPrint = null,
): ?string

Parameters:

ParameterTypeDescription
$pathstringThe full file path to the image (supported formats: JPEG, PNG, GIF, WEBP).
$asciistringA string of ASCII characters used to represent different brightness levels
(default: Image::ASCII_CLASSIC).
$widthint|nullOptional target width to resize the image (default: null).
$heightint|nullOptional target height to resize the image (default: null).
$grayscaleboolWhether to use a weighted grayscale for enhanced contrast based on the ASCII character representation (default: false).
$colorsarray<string|int, string>Foreground colors in character order or keyed by character (e.g, [' ' => 'red', '@' => 'blue', '#' => 'green']).
$lazyPrintarray{pixel?: int, pixels?: int}|int|nullOptional lazy printing effect configuration (default: null).

Return:

string|null - Returns the generated art as a string by default. Successful delayed output streams directly to the terminal and returns null. An unreadable or unsupported file returns an error message.


colors

Maps characters from an ASCII string to corresponding colors.

This method takes an ASCII string and an array of colors, mapping each character in the ASCII string to a color from the array.

public static function colors(string $ascii, array $colors): array

Parameters:

ParameterTypeDescription
$asciistringA string of ASCII characters to be mapped.
$colorsstring[]A list of foreground colors in the order of the characters.

Return:

array<string|int, string> - Maps characters to their corresponding colors. PHP uses integer keys for numeric characters such as 0. Mapping stops when the list of colors or the ASCII string runs out.

Example:

This will return an associative array where each ASCII character is mapped to a corresponding color from the provided $colors array:

use Luminova\Command\Utils\Image;

$ascii = '+#0 ';
$colors = ['blue', 'red', 'white', 'green'];

$mapping = Image::colors($ascii, $colors);

print_r($mapping);

Example Output:

Each character in $ascii (+, #, 0, and a space) is mapped to a color from $colors. The result provides a color-coded set of ASCII characters, useful for creating colorful ASCII art.

Array
(
    [+] => blue
    [#] => red
    [0] => white
    [ ] => green
)