Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sniffabyte's avatar

using Faker to fake images always returns false

Hello guys I'm trying to use Faker to populate images on my localhost but it always return false. this is my code

use Faker\Factory as Faker;

$faker = new Faker;

$faker::create()->image('public/images', 400, 300, null, false); // This returns false instead of the created image name.

I tried is with tinker and hardcoded in my code and always returns the same thing. I think it's because of permissions or something like that. I'm using kali linux. I also tried sudo chmod -R 777 on my folders but same problem still exists. Can anyone here help me?

0 likes
6 replies
tykus's avatar

The problem is http://lorempixel.com is currently down, and Faker uses this lorempixel site to generate the images.

Unfortunately, this is not configurable from outside the package, but you could find the vendor/fzaninotto/faker/src/providers\Image.php and change the $baseUrl property to something with a similar public API which does work, e.g. https://picsum.photos:

$baseUrl = "https://picsum.photos/";

Going into the vendor directory is normally something I would not recommend, but since this is for local development only, it should be fine. Caveat, I don't know that the Picsum API is 100% consistent with LoremPixel, so you might get some errors.

greynosa1's avatar

ok unfortunately I couldn't correct the faker error but nevertheless I was able to generate the image on the site that faker is using as default that is the via.placeholder what I did was base myself on the structure of the site through the url it receives to generate the image by creating it through the values of the url creator of the library, to which the following code that I will share creates the generated url valid in the database when entering the field through faker to the table you only copy the url of the table, paste in the browser, download the image in the folder you are using and remove the url from the database and replace it with the name of the image plus the .png of course. I share my code since it was the only way I could get the image, the path and store it.

code: h t t p s :// pastebin. com / DvaudqTW

just remove the spaces since being new does not let me paste the links

greynosa1's avatar

The solution that I have found so far is to modify your image file inside vendor in the path vendor\fakerphp\faker\src\Faker\Provider\Image.php adding the lines that I have below since the creator of via is giving problems in receiving the data via url, to which we can always use the method either imageUrl or simply the image method I leave you my modification below

public const BASE_URL = 'https ://placehold. jp'; // change the url and delete spaces

Once this file is opened, we will search the lines approx 114 and add the following code, let's take the existing code as a reference

curl_setopt($ch, CURLOPT_FILE, $fp); //existing line curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//new line curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//new line $success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;//existing line

2 likes
alangonzaga's avatar

@greynosa1 Amazing, it worked here. Thanks!

// vendor\fakerphp\faker\src\Faker\Provider\Image.php

// public const BASE_URL = 'https://via.placeholder.com'; //existing line
public const BASE_URL = 'https://placehold.jp'; //new line 

curl_setopt($ch, CURLOPT_FILE, $fp); //existing line
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //new line
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //new line
$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; //existing line
2 likes
madadi751's avatar

I was looking all the day but this works thanks a lot

Please or to participate in this conversation.