This package is intended to be used for quickly generating placeholder images with a specific size, color and text. For more complex use cases, you may want to turn to something like stil/gd-text.
- PHP 8.1 or higher
- GD extension
Make sure you require this package in your composer.json:
composer require nicoverbruggen/image-generator
See the example source file that is used to generate and save the sample images. You can generate the following examples:
Please note that for testing purposes, I used Roboto Black as the TrueType font. (This font is not included in this repository.)
Here's a few examples of what you can do with this package:
use NicoVerbruggen\ImageGenerator\ImageGenerator;
(new ImageGenerator())->generate(output: __DIR__ . "/image_example.png", size: '200x200');In addition to saving placeholder images to
use NicoVerbruggen\ImageGenerator\ImageGenerator;
$output = (new ImageGenerator())->generate(output: 'base64', size: '200x200');
echo "<img src='{$output}' alt='Placeholder image'>";A useful use case may be achieved after declare your own helper, like so:
function placeholder_image(string $size = '500x500'): string {
return (new ImageGenerator())->generate(output: 'base64', size: $size);
}This use case can be useful when used in combination with frameworks like Laravel or Symfony:
<div>
<h3>Item</h3>
<img src="{{ placeholder_image('200x200') }}" alt="Placeholder">
</div>
You can also check out the other source file. You can point your browser directly at this file (assuming you're running a PHP server, of course) and it will directly return a file since the path is set to null.
You can also point your PHP installation's webroot to the server directory, and generate images via URL.
- The
sizeparameter is used to size the placeholder images. - The
background_colorparameter is used to set the background color. - The
text_colorparameter is used to set the color of the text (of the dimensions).
Please note that you should not use # in the URL for the hexadecimal notation for colors!
You can then link to the domain you're using to host these placeholders.
For example, if it is image-generator.test:
<div>
<h3>Item</h3>
<img src="https://image-generator.test/?size=500x500&background_color=005577&text_color=FFF" alt="Placeholder">
</div>
If you do not supply a TrueType font path:
- you will be limited in font size options (1 through 5)
- you will not be able to render multiline text
In ImageGenerator, makePlaceholderImage() has been removed. You need to replace all usages of it with generate().
In ImageGenerator, generate()'s path parameter has been replaced with output. If you use named parameters, you will need to update your usage of this method.
It works the same way, but you have more options for this parameter now, in particular base64 is now a valid value for that parameter.
./vendor/bin/phpunit tests
I am not planning to expand the features of this package at this time. If you've made an improvement or fixed something, you are free to send me a pull request.
MIT.
See also: LICENSE.

