You can use ngx-image-cropper as front end.
HTML
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="4 / 3" format="png" (imageCropped)="imageCropped($event)" (cropperReady)="cropperReady()" (loadImageFailed)="loadImageFailed()"></image-cropper>
Component:
import { ImageCroppedEvent, base64ToFile } from 'ngx-image-cropper';
productImageToUpload: any;
imageChangedEvent: any = '';
croppedImage: any = '';
fileChangeEvent(event: any): void {
this.imageChangedEvent = event;
}
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
var originalimageFile = this.imageChangedEvent.target.files[0];
if(originalimageFile){
var productImageToUpload = new File([base64ToFile(this.croppedImage)], originalimageFile.name, {lastModified: originalimageFile.lastModified, type: originalimageFile.type});
}
}
cropperReady() {
}
loadImageFailed() {}
addProductImage() {
//call this on any button click
this.productImageService.uploadProductImage(this.productImageToUpload).subscribe(
res => {
}, err => {
}
);
}
Service:
public uploadProductImage(productImageToUpload:any): Observable<any> {
let body = new FormData();
body.append("productImage", productImageToUpload);
return this.http.put("/upload", body);
}
Spring Backend:
@RequestParam("productImage") MultipartFile productImage