Edit uploaded image with Intervention

user3792623

I am adding the ability to edit an image that is already uploaded on a shopping cart app I am building. I am able to upload an image fine. I am also able to edit every field perfectly except for the image file upload.

I have tried using the same code I have in the upload function to basically upload another image over the existing one but it does not work and throws an error

Call to a member function getClientOriginalExtension() on a non-object

I am using the Laravel framework with intervention and went to the main site of intervention and it does not show an update or edit method. It just shows a delete method.

I need some help in figuring out how to update the image in my postEdit function.

I really have tried everything I know and have researched this with google and cannot figure this out.

My problem lies in this line of code:

File::update('public/'.$product->image);

Thank you for the right direction.

Here is the image upload function (which works perfect)

public function postCreate() {
    $validator = Validator::make(Input::all(), Product::$rules);

    if ($validator->passes()) {
        $product = new Product;
        $product->category_id = Input::get('category_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');

        $image = Input::file('image');
        $filename  = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('img/products/' . $filename);
        Image::make($image->getRealPath())->resize(468, 249)->save($path);
        $product->image = 'img/products/'.$filename;
        $product->save();

        return Redirect::to('admin/products/index')
            ->with('message', 'Product Created');
    }

    return Redirect::to('admin/products/index')
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

Here is the edit image upload function which I cannot get to work.

public function postEdit() {
    $product = Product::find(Input::get('id'));


    if ($product) {
        File::update('public/'.$product->image);
        $product->update(Input::all());
        return Redirect::to('admin/products/index')
        ->with('message', 'Product Updated');
    }

    return Redirect::to('admin/products/index')
        ->with('message', 'Something went wrong, please try again');
}
Matt Burrow

First there is no method for the File facade called update.

You have to reprocess the image to update it.

Secondly, the error is being thrown from the upload. This could be because the image is not being sent through the form properly.

Make sure you have files attribute on your form open.

{{ Form::open(array('route'=> array('aroute'),'method' => 'post', 'files' => true)) }}

If your file is still not being sent check your php.ini setting as the file size of the image could be greater of that set in the post_max_size or upload_max_filesize to a value greater than that size.

Also the change the line;

$path = public_path('img/products/' . $filename);

to

$path = public_path() . 'img/products/' . $filename;

And to edit the image with intervention you need to save the file.

Use;

$image->move($path);

then you can do;

Image::make($path)->resize(468, 249)->save($path);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Edit Uploaded Image in symfony

Uploaded Image cant Edit in edit view

How to edit uploaded image with minimagick, that image is uploaded to active_storage?

how to resize image and reorient it if it rotates when uploaded through IOS devices using image intervention package?

Intervention Image Library - whenever a wrong image format is uploaded, I want to be able to catch it and send it to frontend in Laravel

Image Intervention Image on Laravel

Intervention/Image requires FileInfo

Intervention Image broken in Vue

Replace a color in intervention image

Laravel image intervention compression

Errors with intervention\image

Intervention image on Heroku

image Intervention error

intervention image in Laravel 8

Large images not being uploaded with Intervention in Laravel

Laravel Image Intervention avoid rotation

Image Validation in Laravel 5 Intervention

image intervention out of memory - laravel

Installing Intervention Image without composer

Using Image Intervention with Laravel Nova

How to open an image at a URL with Intervention

Unable to install intervention/image library

When editing a record that has a previously successfully uploaded image, how do I not have to re-upload on edit?

Get Uploaded image back in Edit View Textboxfor input type file MVC5

Intervention image multiple image upload in laravel

Laravel Progressive Image using Image intervention

Intervention Image degrades quality of image on resize

force download image as response lumen + intervention image

How to output the cached image with Intervention Image Cache?