Ad

Store An Image In October CMS Database After Resizing It With Image Intervention Package

- 1 answer

I try to store a user avatar in the October CMS database after resizing it with intervention image package I use this code:

if (Input::hasFile('avatar')) {
    $file= $user->avatar;
    $filenamewithextension = $file->getClientOriginalName();
    //get filename without extension
    $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
    //get file extension
    $extension = $file->getClientOriginalExtension();
    //filename to store
    $filenametostore = $filename.'_'.uniqid().'.'.$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;
}

but I get an error:

Call to undefined method October\Rain\Database\QueryBuilder::getClientOriginalName()

can anyone help me please?!

Ad

Answer

You are calling getClientOriginalName() on the wrong object, try changing the following lines in your code to this:

if (Input::hasFile('avatar')) {
    $file = Input::file('avatar');
    /* ... */
    Storage::put('public/profile_images/'. $filenametostore, fopen($file->getRealPath(), 'r+'));
    Storage::put('public/profile_images/thumbnail/'. $filenametostore, fopen($file->getRealPath(), 'r+'));
    /* ... */
}
Ad
source: stackoverflow.com
Ad