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

Andreas94's avatar

Save image from remote URL

Hi guys

I'm trying to extract an image from an API.

With the variable $game->cover->url I get //images.igdb.com/igdb/image/upload/t_thumb/csbqnva4b4nzdjgkbe8c.jpg, I would like this image in /upload /cover but every tenativo I do I fail him.

This is my code:

$cover = file_get_contents($game->cover->url);
$filename = rand().'-.'.$cover;
Image::make($cover)->save(public_path('/upload/cover' . $filename));
$platform->immagine = '/upload/cover/' . $name_cover;
dd($platform);

But I get the error LOG.info: [INVALID UTF-8 DATA]

I also tried with:

$cover = file_get_contents($game->cover->url);
$name_cover = substr($cover, strrpos($cover, '/upload/cover/') + 1);
Storage::put($name_cover, $cover);
$platform->immagine = '/upload/cover/' . $name_cover;
dd($platform);

But with this new code I get the error: LOG.info: file_get_contents(//images.igdb.com/igdb/image/upload/t_thumb/csbqnva4b4nzdjgkbe8c.jpg): failed to open stream: No such file or directory

How do I save the image of the URL in my server?

0 likes
5 replies
nexxai's avatar

Have you tried adding "https:" to the from of the URL to see if it's related to a missing scheme?

Andreas94's avatar

Again error LOG.info: [INVALID UTF-8 DATA] with:

$cover = file_get_contents('https:'. $game->cover->url);
Snapey's avatar

you are getting confused

$cover = file_get_contents($game->cover->url);
$filename = rand().'-.'.$cover;

following the first line $cover contains the binary data of the image. You cannot use this in your filename

Andreas94's avatar

Thanks @snapey for the supporto. Now i have this error:

$cover = file_get_contents('https:' . $game->cover->url);
$filename = rand().'-.'.str_slug($game->name, '-');
Image::make($cover)->save(public_path('/upload/cover/' . $filename));
$platform->immagine = '/upload/cover/' . $filename;

LOG.info: Encoding format (simcity) is not supported.

simcity is $game->name

I think I went into confusion...

Edit

I solved with:

Image::make($cover)->save(public_path('/upload/cover/' . $filename . '.jpg'));

Is it possible to take the original extension? getClientOriginalExtension()it does not work

Snapey's avatar

The original extension is in $game->cover->url?

You need to parse it from the string.

Please or to participate in this conversation.