Image Laravel4从url获取图像

Image Laravel4从url获取图像,image,upload,laravel,laravel-4,Image,Upload,Laravel,Laravel 4,好的,当我想上传一张图片的时候。我通常会这样做: $file = Input::file('image'); $destinationPath = 'whereEver'; $filename = $file->getClientOriginalName(); $uploadSuccess = Input::file('image')->move($destinationPath, $filename); if( $uploadSuccess ) { // save the

好的,当我想上传一张图片的时候。我通常会这样做:

$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);

if( $uploadSuccess ) {
    // save the url
}
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
当用户上传图像时,这可以正常工作。但是如何从URL保存图像

如果我尝试以下方法:

$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);

if( $uploadSuccess ) {
    // save the url
}
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
然后:

$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
我得到以下错误:

Call to a member function move() on a non-object
那么,如何从laravel 4的URL上传图像


Amy非常感谢您的帮助。

我认为只有当您通过POST请求上传文件时,才会使用Laravel的Input::file方法。您得到的错误是因为file\u get\u内容没有返回您laravel的类。您不必使用move()方法,也不必使用类似的方法,因为您从url获取的文件不会上载到您的tmp文件夹

相反,我认为您应该使用这里描述的内容

比如:

我现在无法检查,但这似乎是一个不错的解决方案。只需从url获取数据,然后将其保存到您想要的任何位置

        $url = "http://example.com/123.jpg";
        $url_arr = explode ('/', $url);
        $ct = count($url_arr);
        $name = $url_arr[$ct-1];
        $name_div = explode('.', $name);
        $ct_dot = count($name_div);
        $img_type = $name_div[$ct_dot -1];

        $destinationPath = public_path().'/img/'.$name;
        file_put_contents($destinationPath, file_get_contents($url));
这将把图像保存到您的/public/img,filename将是原始文件名,在上述情况下为123.jpg


中引用的“获取图像”名称我不知道这是否会对您有很大帮助,但您可能希望查看。它最初打算用作图像处理库,但它提供了从url保存图像的功能:

$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');

$new必须是一个文件名,而不是目录。此包执行此任务。