9951 explained code solutions for 126 technologies


php-fakerHow can I generate a fake image using PHP Faker?


Generating fake images with PHP Faker is possible with the help of the Faker\Provider\Image class. This class provides methods for generating a variety of fake images with different sizes, formats, and backgrounds.

Below is an example of how to generate a fake image using PHP Faker:

<?php

require_once 'vendor/autoload.php';

$faker = Faker\Factory::create();

$image = $faker->image(
    'public/images', // directory where image will be saved
    640, // image width
    480, // image height
    'cats', // category of image
    false, // whether or not to randomize image name
    true, // whether or not to return the full path to the image
    'Faker' // text to be written on the image
);

echo $image;

// Output: public/images/cats/640x480_Faker.jpg

The code above will generate an image with a width of 640px, a height of 480px, and the text Faker written on it. The image name will be a random string and the full path to the image will be returned.

The Faker\Provider\Image class also provides other methods for generating images such as imageUrl, imageUrl, unsplashImage, and image. The image method can be used to generate an image with a random background, width, height, and name.

Helpful links

Edit this code on GitHub