Webiondev123's avatar

Stuck in uploading image by intervention

Unable to insert image in either using store or save. Using store says store function not available to GD...using save says not correct string value

public function uploadfile(){

        
        // $manager = new ImageManager(array('driver' => 'imagick'));
         $request= Req::capture();  // This gives you the current 

            //get file
              $file =$request->file('file');
             $file=Image::make($file)->resize(128, 128);
            // $file = $request->file('file')->store("public"); 
          
             $file= $file->save("public");  
           
            return $file;

        }
0 likes
7 replies
Snapey's avatar

I think you need to give the full path of where to save the file and what to call it, not just 'public'

Webiondev123's avatar

I changed the type to blob in mysql table then it works for save but I cannot read the image however and it is not being stored in laravel folder

kendrick's avatar

You could try:

            $file =$request->file('file');      
    
            $filename = time() . '.' . $file->getClientOriginalExtension();
                $location = public_path('images/'. $filename);
                Image::make($file)->resize(128,128)->save($location);

                $file->file = $filename;
                $file->save();  

This would store your file within a folder (/images) in your public directory @Webiondev123

1 like
Webiondev123's avatar

@splendidkeen It saves but when I try to read it outputs garbage

src="{{ auth()->user()->file}} //I want to display image

var_dump(auth()->user()->file

��Y���z�=������m��ȱ��{��? ��\]�^�<]�O8a]�6�*ܔ���d�v88�k���{\ɴ~��r��#�Z��ӊ��sҷR��kf���Ó�5|�)F^��ݨ}ppF�
Snapey's avatar

You don't write to the database in the code you have shown. Anything coming back from user->file must have been garbage from when you saved as blob in the database.

Webiondev123's avatar

@Snapey

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'city'=>'required|string|max:255',
            'country'=>'required|string|max:255',
            'type'=>'required',
            'occupation'=>'required|string|max:255',
            'file' => 'required | image',

        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */

    public function uploadfile(){

        
          $request= Req::capture();  // This gives you the current 

         //    //get file
         //    $file =  $request->file('file')->store("public"); 
          
         //    return $file;

         $file =$request->file('file');      
    
            $filename = time() . '.' . $file->getClientOriginalExtension();
                $location = public_path('img/'. $filename);
                $file=Image::make($file)->resize(128,128)->save($location);

                $file->file = $filename;
                //$file->save();  
                return $file;
        }
 
    protected function create(array $data)
    {
        $file=$this->uploadfile();
        
        
        //$file=explode("/", $file);
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'city'=> $data['city'],
            'country'=>$data['country'],
            'type'=>$data['type'],
            'occupation'=>$data['occupation'],
            'file'=>$file,
            
        ]);
    }


```
Snapey's avatar
Snapey
Best Answer
Level 122

So returned from uploadFile is an instance of Intervention image. You store this in the database, so what you are looking at in the database is the binary of the image.

If this is not what you wanted, return the filename from uploadFile instead. Probably also change the database column type.

Please or to participate in this conversation.