Mostly, on local environment, while trying to populate images in Laravel
using Faker
you might face an issue, when faker returns false every time.
If we check the file /vendor/fakerphp/faker/src/Faker/Provider/Image.php
will see that image service provider link is:
https://via.placeholder.com
The problem mostly related with cURL, when trying to verify certificate and fails.
So the solution is to change the BASE_URL constant from https://via.placeholder.com
to http://via.placeholder.com
or put the next code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
under image
() method as there is no other way to configure it.
But still, if we don’t want to touch the vendor package file, we can use the following code to populate the file:
$faker = Faker\Factory::create();
$imageUrl = $faker->imageUrl();
$contextOptions = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
$image_content = file_get_contents($imageUrl, stream_context_create($contextOptions));
$image = Storage::disk('public')->put('image.png', $image_content) ? Storage::disk('public')->path('image.png') : false;