Laravel 4 从 url 获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17776291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 get image from url
提问by user1543871
OK so when I want to upload an image. I usually do something like:
好的,所以当我想上传图像时。我通常做这样的事情:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
当用户上传图像时,这工作正常。但是如何从 URL 保存图像???
If I try something like:
如果我尝试类似的事情:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
进而:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
我收到以下错误:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
那么,如何使用 laravel 4 从 URL 上传图像?
Amy help greatly appreciated.
非常感谢艾米的帮助。
回答by dcizal
I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
我不知道这是否会对你有很大帮助,但你可能想看看干预图书馆。它最初打算用作图像处理库,但它提供了从 url 保存图像的功能:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
回答by Ninjoe Quah
$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));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
这会将图像保存到您的 /public/img,文件名将是原始文件名,即上述情况下的 123.jpg。
the get image name referred from here
从此处引用的获取图像名称
回答by Victor
Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
我认为 Laravel 的 Input::file 方法仅在您通过 POST 请求上传文件时使用。你得到的错误是因为 file_get_contents 没有返回你的 laravel 类。而且您不必使用 move() 方法或它的模拟方法,因为您从 url 获取的文件不会上传到您的 tmp 文件夹。
Instead, I think you should use PHP upload an image file through urlwhat is described here.
相反,我认为您应该使用PHP 通过此处描述的url 上传图像文件。
Like:
喜欢:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want
我现在无法检查它,但这似乎是一个不错的解决方案。只需从 url 获取数据,然后将其保存在您想要的任何位置