0

I'm trying to create a working image upload in a form using Laravel 5 and Intervention.

    public function store()
    {
    $input = Request::all();
    Project::create($input);

    if (Input::hasFile('image'))
    {
        $file = Input::file('image');
        $file->move('uploads', $file->getClientOriginalName());

        $image = Image::make(sprintf('uploads/%s', $file->getClientOriginalName()))->resize(120, 120)->save();
    }

    return $file;

   // return redirect('projects');
}

It's copying the image in the specified folder but when I return $file it gives me something in the lines of

/tmp/phpoMwrrD

Instead of the file name. I've been looking over the Intervention documentation, but can't find what I'm doing wrong.

Quoting the documentation;

Handling image uploads in Laravel

In a Laravel application it is also possible to pass an uploaded file directly to the make method.

Creating Image from File Upload in Laravel

// resizing an uploaded file Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');

Thanks in advance.

Joren Polfliet
  • 127
  • 3
  • 13
  • Where do you return `$file`? Why don't you work with the `$image`? – Glad To Help Oct 13 '15 at 14:50
  • @GladToHelp Updated OP with the entire function. I could work with $image, but in the progress of writing the function started using $file and didn't change. It shouldn't change the result of the function though. – Joren Polfliet Oct 13 '15 at 14:54
  • 1
    The controller tries to convert the `$file` object to string, that is why you see the temporary name/path. You should take the filename from `$file->getClientOriginalName()` – Glad To Help Oct 13 '15 at 14:57
  • Isn't that whats it doing here '$file->getClientOriginalName()))->resize(120, 120)->save();' or am I completely wrong on this one? It's resizing the file and saving the $file->getClientOriginalName() to the database, right? The sprintf is returning a formatted string, might that be the problem? I've been using http://www.phpgang.com/image-upload-and-resize-using-image-intervention-in-laravel_889.html as a tutorial for this. – Joren Polfliet Oct 13 '15 at 15:04
  • your complaint was that: "$file it gives me something in the lines of /tmp/phpoMwrrD instead of the file name." If you want the file name, get it from the string, don't output the whole $file object because Laravel will try to invoke toString() method on it, which in turn outputs the temp file/path. Maybe you want to achieve something else? – Glad To Help Oct 13 '15 at 15:07
  • I see what you're saying. When I return $image, I suppose it really does return the entire image because I just get a page of weird symbols. It's also saving the /tmp/blabla to the database, which to me seems weird. – Joren Polfliet Oct 13 '15 at 15:22

2 Answers2

0

Fixed the issue thanks to @GladToHelp. Used

`$imageN = $file->getClientOriginalName();`

To retrieve the file name.

Joren Polfliet
  • 127
  • 3
  • 13
0

try this

   if (Input::hasFile('image'))
{
    $file = Input::file('image');

    $image = Image::make($file)->resize(120, 120)->save('uploads/'.$file->getClientOriginalName());
}
Mohammed Aktaa
  • 1,345
  • 9
  • 14