Ad

Save An Avatar In Octobercms Database After I Modified It

- 1 answer

after resizing the user avatar with intervention image ,i try to store it in the octobercms database like this code:

        if (Input::hasFile('avatar')) {


    $file= Input::file('avatar');

  $filenamewithextension =   $file->getClientOriginalName();


   //get filename without extension
           $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);


  //get file extension
      $extension = $file->getClientOriginalExtension();


   //filename to store
           $filenametostore = $filename.'_'.time().'.'.$extension;





            Storage::put('public/profile_images/'. $filenametostore, fopen($file, 'r+'));
            Storage::put('public/profile_images/thumbnail/'. $filenametostore, fopen($file, 'r+'));



    //Resize image here
           $thumbnailpath ='storage/app/public/profile_images/thumbnail/'.$filenametostore;


             $img = Image::make($file->getRealPath());
             $img->crop(request('w'), request('h'), request('x1'), request('y1'));


      $img->save($thumbnailpath);

        $user->avatar=  $filenametostore;


        }

i get this error:

The avatar must be an image.
C:\wamp643\www\october3\vendor\october\rain\src\Database\Traits\Validation.php line 340

i really don't know what to do ,i'm a beginner.

please help me!!

Ad

Answer

you are just setting file name you should set full path of image there.

Assuming you have already avatar attachment relation in user model

public $attachOne = [
    'avatar' => ['System\Models\File']
];

replace

$user->avatar = $filenametostore;

with

$user->avatar = storage_path($thumbnailpath);

may be it should work.

if doubt please comment.

Ad
source: stackoverflow.com
Ad