standaniels/image-generator

A package to generate random images.

Maintainers

Package info

github.com/standaniels/image-generator

Language:C

pkg:composer/standaniels/image-generator

Statistics

Installs: 96 941

Dependents: 1

Suggesters: 0

Stars: 15

Open Issues: 0

1.1.0 2021-04-07 13:31 UTC

This package is auto-updated.

Last update: 2026-03-29 07:55:03 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

This package makes generating images easy. Use them for placeholders without being dependent on some external service.

use StanDaniels\ImageGenerator\Canvas;
use StanDaniels\ImageGenerator\Color;
use StanDaniels\ImageGenerator\Image;
use StanDaniels\ImageGenerator\Shape\Shape;

$transparency = random_int(60, 80) / 100;
$canvas = Canvas::create(400, 400, 2)
    ->background(Color::random($transparency));

for ($i = random_int(100, 150); $i > 0; $i--) {
    $transparency = random_int(60, 80) / 100;
    Shape::random($canvas, Color::random($transparency))->draw();
}

// By default, the image is stored in the directory used for temporary files
$image = Image::create($canvas);

Of which this could be the output:

A randomly generated image

Using color palettes

If you would like to generate an image based on a given set of colors like the one below, you can do it like this.

Color palette

use StanDaniels\ImageGenerator\Canvas;
use StanDaniels\ImageGenerator\Color;
use StanDaniels\ImageGenerator\Image;
use StanDaniels\ImageGenerator\Shape\Shape;

$colors = [
    new Color(73, 78, 109),
    new Color(214, 119, 98),
    new Color(144, 180, 148),
    new Color(237, 203, 150),
    new Color(136, 80, 83),
];

$canvas = Canvas::create(400, 400, 2)
    ->background(new Color(34, 36, 50));

for ($i = random_int(50, 100); $i > 0; $i--) {
    $color = clone $colors[random_int(0, count($colors) - 1)];
    $color->setAlpha(random_int(50, 60) / 100);
    Shape::random($canvas, $color)->draw();
}

$image = Image::create($canvas);

The output would be something like this:

A randomly generated image based on a given set of colors

Installation

The library is distributed as a C extension. You compile it once against your local PHP installation, then load it like any other .so extension.

Requirements

Requirement Notes
PHP ≥ 8.0 Tested through PHP 8.5
php-dev / php-devel Provides phpize and the build headers
libgd PHP must be built with GD support (ext-gd)
libexif PHP must be built with EXIF support (ext-exif)
A C compiler gcc or clang
make Standard build tool

On Debian / Ubuntu:

sudo apt-get install php-dev php-gd php-exif build-essential

On Red Hat / Fedora / CentOS:

sudo dnf install php-devel php-gd php-exif gcc make

On macOS (with Homebrew):

brew install php
# php-config, phpize, and gd are included in the Homebrew php formula

Build and install

The C source lives in the ext/ directory.

cd ext/

# 1. Prepare the build environment
phpize

# 2. Configure (links against your active PHP installation automatically)
./configure

# 3. Compile
make

# 4. Install the .so into your PHP extension directory
sudo make install

make install copies image_generator.so to the directory shown by php-config --extension-dir.

Enable the extension

Add the extension to your php.ini:

extension=image_generator

Find the right php.ini with:

php --ini

To enable it only for a single script without editing php.ini:

php -d extension=image_generator script.php

Verify the installation

php -r "var_dump(extension_loaded('image_generator'));"
# → bool(true)

Run the test suite

The PHPT tests in ext/tests/ are run with PHP's built-in test runner. From inside the ext/ directory (after make):

make test

Or run them directly against the freshly built .so:

php run-tests.php -d extension=modules/image_generator.so tests/

License

The MIT License (MIT). Please see License File for more information.