0

I have an API project using Laravel and these requires me to have a download and save it to public folder and in database.

Flow:

A field will come through the API call, i.e. through the company_logo field. It will come through as a URL: http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg. I need to grab the image and save it locally to a unique folder per listing and save it also to database.

I don't have code yet because I don't know what to do here in Laravel.

halfer
  • 19,824
  • 17
  • 99
  • 186
angel1108
  • 333
  • 4
  • 22

2 Answers2

1

Hope this help, you need to define $url and $filename variable

$data = file_get_contents( $url );
file_put_contents( public_path($filename), $data ); 
CodexVarg
  • 502
  • 1
  • 6
  • 20
1

You can simply do that by using Intervention Image

You can find how to install here

$path = 'http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg';
$filename = basename($path);
Image::make($path)->save(public_path('yourimagefolder/' . $filename));

this will save the image. and following code will save it to database

$image = new ModelName();
$image->imagename  =$filename;
$image->save();
stoneshaq
  • 316
  • 3
  • 18