Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 get image from url

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???

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??

Amy help greatly appreciated.

like image 492
user1543871 Avatar asked Dec 08 '22 14:12

user1543871


2 Answers

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:

$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
like image 64
dcizal Avatar answered Dec 11 '22 09:12

dcizal


        $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.

the get image name referred from here

like image 23
Ninjoe Quah Avatar answered Dec 11 '22 09:12

Ninjoe Quah