Generator of placeholder images for Faker
Generator of placeholder-type images using GD for FakerPHP/Faker
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.
- By using an external source of data, image generation needs to be done on a connected environment. It might be ok for most usage, but can be a problem if you need to generate data on the go or somewhere not connected to the internet.
- Also, because of the same reason, getting a lot of pictures can take a very loooong time as it downloads every picture. If you want to fill your data with hundreds of pictures, it might be problematic.
- Finally, LoremPixel and Placeholder only generates JPEG pictures, that can takes a large amount of storage.
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:
- Fast image generation
- No need of internet connection
- Can generate both JPEG and PNG pictures
Drawbacks of using images-generator:
- Can't generate realistic pictures: That would be more complex to do (and slower). If you need real content, the only option for now is to stick with LoremPixel content.
The easiest way is to use composer:
composer require --dev maltyxx/images-generator
Or if you prefer by modifying your composer.json file, add this line to the "require" section:
ImagesGeneratorProvider class in combinaison with Faker to produce new images:
<?php require __DIR__ .'/vendor/autoload.php'; use Maltyxx\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:
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)
- $dir: (string) Path of the generated picture file. Must be writable. If omited, will default to the system temp directory (usualy /tmp on Linux systems).
- $width: (integer) width in pixels of the picture, default to 640.
- $height: (integer) height in pixels of the picture, default to 480.
- $format: (string) format of the output picture file, can takes
jpg, jpeg or pngas valid values, default to png.
- $fullPath: (boolean) If true, returns the full path of the file generated, otherwise will only return the filename, default to true.
- $text: (string) If given, will output the text on the picture. Will try to make the font-size fit the picture size. Default to null (no text). A special value can be used, if set to
true, will return the width and height as text of the picture (example:
- $backgroundColor: (string) The hexadecimal color value (such as
'1f1f1f'), default to black.
- $textColor: (string) The hexadecimal color value (such as
'ff2222'), default to white.
$faker->imageGenerator(null, 640, 480, 'png', false, true, '#1f1f1f', '#ff2222')Will generate a 640x480 dark grey png picture with the text '640x480' in red and return the filename like this:
$faker->imageGenerator('img', 640, 480, 'png', true, 'Faker', '#0018ff', '#ffd800')Will generate a 640x480 blue png picture with the text 'Faker' in yellow inside an
imgdirectory of your project and return the full path such as
More complex example using the power of Faker:
$faker->imageGenerator('img', $faker->numberBetween(600, 800), $faker->numberBetween(400, 600), 'jpg', true, $faker->word, $faker->hexColor, $faker->hexColor)Will return a jpeg of a random color picture with a width between 600 and 800 pixels, height between 400 and 600 pixels, with a random word written in a random color! This could generate pictures like theses:
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);
This is the first release, and it already should work for most usages. But I already have some goals for future releases to come:
- Add support for ImageMagick and/or GraphicsMagick for faster generation of pictures.
- Add support for images-generator in Nelmio/Alice.
- Add the ability to generate identicon type of pictures
- Add unit tests
Otherwise, any suggestion is welcome! You can fork this project and submit your PR.
There are no unit tests included currently (see Future developments).
images-generator is released under the MIT Licence. See the bundled LICENSE file for details.