How to Generate Fake Image URLs in PHP Using Faker

Share this Article
Reading Time: 5 minutes
275 Views

While working in any application, sometimes we need lots of dummy data to test the workflow of entire application. Inside this article we will see how to generate fake image urls in php using faker library.

Faker library needs to be install by using composer. Once install, there are several properties, methods available to generate various type of fake data like Name, Email, Phone number, Image Urls etc.

This article is super easy to understand and implement in php programming. To use Faker library, your system must have composer installed. If don’t, please follow this article to install in Linux Operating System. How To Install Composer in Ubuntu 18.04 ?

Learn More –

To do for this article –

  • Create a PHP Application
  • Use Faker Library to generate image urls

Let’s get started.


Create an Application

Create a folder with name php-faker. Inside this folder create these files –

  • composer.json
  • index.php

We need composer.json file because we will install faker library. Write this curly pairs in composer.json file.

{}

If you don’t write this, probably you will get this error on terminal when we install composer package.


Install Faker Library

Open project in terminal and run this command.

$ composer require fzaninotto/faker

After installation of this package, automatically composer.json file get updated.

{
    "require": {
        "fzaninotto/faker": "^1.9"
    }
}

Also you will see inside your application setup, you have now a vendor folder. This is package installed folder, don’t do anything with that.


Load Faker Library to Application

Open index.php file and use require_once php function to include faker library.

<?php

require_once 'vendor/autoload.php';

// use the factory to create a Faker\Generator instance 
$faker = Faker\Factory::create();

...

Use Faker\Factory::create() to create and initialize a faker generator.


Generate Image URLs Using Faker

Open index.php and write this complete code into it.

<?php

require_once 'vendor/autoload.php';

// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();

for ($i = 0; $i < 10; $i++) {

    $width = 100;
    $height = 100;

    $image = $faker->imageUrl($width, $height, 'cats');
    echo  "URL - ".$image . "<br/>";
    echo " -- Image -- <br/>";
    echo "<img src='" . $image . "'/>";
    echo "<br/>";
}

Output

Explanation of Image Method

To generate image urls using faker we have $faker->imageUrl($width, $height, $category);

imageUrl() is the method which we can call by $faker instance. This method takes width, height and a image category.

Supported Image Categories of Lorem Ipsum are –

animals, cats, nature, …

Example #1

http://lorempixel.com/200/400/animals/

Example #2

http://lorempixel.com/200/400/food/

We hope this article helped you to learn How to Generate Fake Image URLs in PHP Using Faker in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Read More Articles

Leave a Comment