Home

Awesome

images-generator

Generator of placeholder-type images using GD for fzaninotto/Faker

Sample image

Build Status

What is the goal of this project?

Faker is an amazing tool to quickly generate bunch of fake data that looks real. You can refer to its documentation to learn more about the powerful things it can do.

One of the great option of Faker is to be able to use LoremPixel to get pretty pictures. It works great but has a few drawbacks:

images-generator comes in order to solve this problem. All pictures are directly generated by your current PHP setup using GD, the almost standard for image manipulation in PHP.

images-generator can gets very useful for unit tests, integration servers, fixtures and many other cases...

Advantages of using images-generator:

Drawbacks of using images-generator:

Installation

The easiest way is to use composer:

composer require bheller/images-generator

Or if you prefer by modifying your composer.json file, add this line to the "require" section:

"bheller/images-generator": "~1.0"

Usage

Use the ImagesGeneratorProvider class in combinaison with Faker to produce new images:

<?php
require __DIR__ .'/vendor/autoload.php';

use bheller\ImagesGenerator\ImagesGeneratorProvider;

$faker = Faker\Factory::create();
$faker->addProvider(new ImagesGeneratorProvider($faker));
$image = $faker->imageGenerator();
?>
<img src="<?php echo $image; ?>">

It will generate a black png picture of 640*480 pixels inside your temp directory (if available) and return the full path of the picture like this: '/tmp/13b73edae8443990be1aa8f1a483bc27.png'.

Parameters

The imageGenerator() method has many arguments. Here are the default values:

imageGenerator($dir = null, $width = 640, $height = 480, $format = 'png', $fullPath = true, $text = null, $backgroundColor = null, $textColor = null)

Description:

Examples:

Sample image

Sample image

More complex example using the power of Faker:

Sample image

Seeding the generator

The images-generator doesn't directly uses the seeding power of Faker. But you still can use it to generate the different parameters values. For example, this code will always output the same green picture with the same text (dolorum):

$faker->seed(1234);
$image = $faker->imageGenerator('img', $faker->numberBetween(600, 800), $faker->numberBetween(400, 600), 'jpg', true, $faker->word, $faker->hexColor, $faker->hexColor);

Sample image

Future developments

This is the first release, and it already should work for most usages. But I already have some goals for future releases to come:

Otherwise, any suggestion is welcome! You can fork this project and submit your PR.

Tests

There are no unit tests included currently (see Future developments).

License

images-generator is released under the MIT Licence. See the bundled LICENSE file for details.

The Roboto font file included in this project is under Apache Licence 2.0 as mentioned on the Roboto repository.