lundi 14 mars 2022

How to edit image in Laravel 5.7?

I inserted the image in my database, now I am trying to edit the images and the edited image should be deleted from my folder and a new image should be updated there. Could you please help me where I am mistaking?

Here is my Controller.php file

 public function edit($slider)
{
    $property=Property::all();
    $slider = Slider::find($slider);
    $data = ['property'=>$property, 'slider'=>$slider];
    return view('admin.slider.edit', $data);
}

 public function update(Request $r, $id)
{
       $slider=Slider::find($id);
       if( $r->hasFile('slider_thumb')){ 

        $thums               = $r->slider_thumb;
        $slider_thumb = uniqid($chr).'.'.$thums->getClientOriginalExtension();
        $img = Image::make($thums->getRealPath());
        $img->resize(204, 107, function ($constraint) {
            $constraint->aspectRatio();          
        });
        $thumbPath = public_path().'/slider_img/'.$slider_thumb;

        if (file_exists($thumbPath)) {
            $this->removeImage($thumbPath);
        } 
        $img->save($thumbPath);
        $optimizerChain = OptimizerChainFactory::create();
        $optimizerChain->optimize($thumbPath);
        $slider_thumbimg = $slider_thumb;

        }else{
            $slider_thumb = NULL;
        }
        $slider->property_id = $r->property_id;
        $slider->slider_image=$slider_imageimg;
        $slider->save();
       return redirect('/admin/slider');
    }

}

here is my HTML file

            <form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
            @csrf
            @method('PUT')

@if($slider->slider_image    == NULL or $slider->slider_image == '')
                                            <img src="" style="width: 100px; height: 100px;">
                                            @else
                                            <img src="" style="width: 100px; height: 80px;">
                                            @endif
                                            <input type="file" name="slider_image" value="">

                <div class="form-group">
                    <div class="col-sm-12 text-right">
                        <button type="submit"  class="btn btn-info">
                            <i class="fa fa-check"></i> Save
                        </button>
                    </div>
                </div>
            </form>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire