How to Generate Fake Image URLs in PHP Using Faker

Reading Time: 5 minutes

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.


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.


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/>";


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

Example #2

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

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more