Reputation: 3393
What I'm Using
What I'm trying to achieve
Uploading an image to firebase (got this down)
Resizing the image before uploading
Where I'm stuck
So I can successfully upload an image to firebase storage
What I'd like to avoid is users uploading images larger than 300 x 300
Question
My Upload Service
Below is how my component successfully uploads to firebase. If anyone can assist on how to resize / reduce the image size before it gets uploaded, I would be exceptionally grateful!
uploadImage(upload: Upload) {
this._subscription = this.authService.user.subscribe(user => {
if (user) {
// Generate a new firebase key
let newPostKey = firebase.database().ref().child('albums').push().key;
// Create a reference to the firebase storage
this.imagePathString = 'thumbnail/' + newPostKey + '/thumbnails';
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(this.imagePathString).put(upload.file);
// Upload task initiated - View progress and update real time database
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100;
console.log(upload.progress);
},
(error) => {
// upload failed
console.log(error)
},
() => {
return undefined;
}
);
}
});
}
Upvotes: 2
Views: 1763
Reputation: 3771
Have you tried ng2-img-cropper ? For demo check this plunkr
Upvotes: 0