Intervention Image: Save image directly from an url with original file name and ext?
Asked Answered
R

3

19

How to get the filename, when taking image from a remote server? And how to save with original size and filename?

// Take remote image
$img = Image::make('http://image.info/demo.jpg');
// how to save in the img/original/demo.jpg
$img->save(????);

I use Intervention, (http://image.intervention.io/api/make) to build CakePHP 3 image Behavior, I want to provide an easy uploading from remote servers, and keep the original image as a source for future manipulation.

EDIT

I ask, is there the Intervention Image method that returns the name of the file, when taken from the remote server. I know php copy(), basename(), I can also use the CakePHP File utilities, but it gives me the duplicate request on remote file.

Reflectance answered 28/9, 2015 at 17:26 Comment(2)
possible duplicate of Copy Image from Remote Server Over HTTPGladsome
No is not duplicate.Reflectance
L
38

response() does not return the original filename like Salines was requesting. This should work

$path = 'https://i.sstatic.net/koFpQ.png';
$filename = basename($path);

Image::make($path)->save(public_path('images/' . $filename));
Landowner answered 29/9, 2015 at 2:7 Comment(0)
D
4

You can easily do it using the make(); and response(); methods. Check my code below.

Just keep in mind that allow_url_fopen must be enabled to fetch a remote image.

This will display my profile picture. If you are not using it with Laravel Framework, just replace return with echo.

$img = Image::make('https://i.sstatic.net/koFpQ.png');
header('Content-Type: image/png');
return $img->response();

Hope that helps and upvote my reply if useful ;)

If you need further help just ask.

Drucilla answered 28/9, 2015 at 19:44 Comment(1)
I like it, no need to temporarily store on server. But I have a question. Is there a way to make it work for files in general? For example, my user can upload either a photo or a pdf document... how would I handle it?Yasmineyasu
T
0

First Create new folder inside public folder called images , and execute the below code to download image from url and save it inside public\images folder

        $url = 'http://example.com/image.png';
        $img = public_path('images') . '\\test.png';
        file_put_contents($img, file_get_contents($url));
    
Twofaced answered 8/11, 2020 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.