standaniels/image-generator

A package to generate random images.

1.1.0 2021-04-07 13:31 UTC

This package is auto-updated.

Last update: 2022-11-27 09:42:42 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

You can install the package via composer:

composer require standaniels/image-generator

License

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