0

I'm working on a RestFulApi using Laravel and Vuejs, now want upload a photo using RestfulApi and vuejs. Here comes my sample code:

<div class="form-group form-group-default float-left required">
<label for="photo">Photo</label>
<input class="file-input" type="file" ref="photo" name="photo"  v-
    on:change="addFile($event)">        
</div>
data(){
return{              
    films_info:
    {                       
        photo: null,
     }
    }
},
methods: {
addFile: function(e){
    let that = this;
    that.films_info.photo = this.$refs.photo.files[0];
},
saveFilms: function(){
    let that = this;
    axios.post('/api/films/save_films',that.films_info)
        .then(function (response) {
             location.reload(true);
         })
         .catch(function (error) {
             that.errors = error.response.data.errors;
             console.log("Error Here: "+error.response.data);
         });
    }
}


protected function saveFilms(Films $request)
{       
 if ( $request->photo ) {
     $extension = $filmsObj->photo->getClientOriginalExtension(); 
            $request->photo = 'films_'.\Carbon\Carbon::now().'.'.$extension; // renaming image
            $request->photo->move($dir_path, $request->photo);                     
  }
}

Here in this code I get error in getClientOriginalExtension() method call. It says:

getClientOriginalExtension() method called on string.

1 Answer 1

1

Finally I managed to solved the photo upload issue using VueJS and Laravel Api call.

<div class="form-group form-group-default float-left required">
    <label for="file">File</label>
    <input class="file-input" type="file" ref="file" name="file"  v-
    on:change="addFile($event)">        
</div>

data(){
    return{              
    films_info:
    {                       
       file: null,
     }
    }
 },
methods: {
    addFile(e){
        this.films_info.file = this.$refs.file.files[0];
    },
    saveFilms(){
        let formData = new FormData();
        formData.append('file', this.films_info.file);
        axios.post('/api/films/save_films',formData,{
                headers: {
                'Content-Type': 'multipart/form-data'
                }
                })
        .then(response => {
            location.reload(true);
         })
        .catch(error => {
            that.errors = error.response.data.errors;
            console.log("Error Here: "+error.response.data);
     });
}

}

$dir_path = 'uploads/films/images';
    $dir_path_resize = 'uploads/films/images/45x45';
    if( $request ){
        $filmsObj = new Film();
        if (!File::exists($dir_path))
        {
            File::makeDirectory($dir_path, 0775, true);
        }
        if (!File::exists($dir_path_resize))
        {
            File::makeDirectory($dir_path_resize, 0775, true);
        }
        if ( $request->file ) {
            $file  = $request->file;
            $extension = $file->getClientOriginalExtension(); // getting image extension
            $file_name = 'films_'.\Carbon\Carbon::now().'.'.$extension; // renaming image
            $file->move($dir_path, $file_name); // uploading file to given path

            // Start : Image Resize
            $image = Image::make(public_path($dir_path.'/'.$file_name));
            // resize the image to a height of 45 and constrain aspect ratio (auto width)
            $image->resize(null, 45, function ($constraint) {
                $constraint->aspectRatio();
            });
            $image->save(public_path($dir_path_resize.'/'.$file_name));
            // End : Image Resize
        }
Sign up to request clarification or add additional context in comments.

2 Comments

hello, Image::make not found is the error I am getting. how can l add the Image class?
Got it. I install Intervention/Image using 'composer require intervention/image'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.