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

前端 未结 3 716
悲&欢浪女
悲&欢浪女 2020-12-14 19:44

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(\'ht         


        
相关标签:
3条回答
  • 2020-12-14 19:58

    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));
        
    
    0 讨论(0)
  • 2020-12-14 20:07

    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.stack.imgur.com/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.

    0 讨论(0)
  • 2020-12-14 20:12

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

    $path = 'https://i.stack.imgur.com/koFpQ.png';
    $filename = basename($path);
    
    Image::make($path)->save(public_path('images/' . $filename));
    
    0 讨论(0)
提交回复
热议问题