Skip to content

Commit

Permalink
Implement #527 resize image on upload (#538)
Browse files Browse the repository at this point in the history
* resize image on upload

* #527 resize only if bigger than 500px
  • Loading branch information
syjer authored and cbellone committed Oct 31, 2018
1 parent 3f6a7e5 commit c6a666f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ dependencies {
compile 'de.danielbechler:java-object-diff:0.95'
compile 'com.github.scribejava:scribejava-core:5.0.0'
compile 'org.apache.poi:poi-ooxml:3.17'
compile 'org.imgscalr:imgscalr-lib:4.2'

testCompile "org.springframework:spring-test:$springVersion"
testCompile "com.insightfullogic:lambda-behave:0.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@

import alfio.manager.FileUploadManager;
import alfio.model.modification.UploadBase64FileModification;
import org.imgscalr.Scalr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
Expand All @@ -38,8 +45,28 @@ public FileUploadApiController(FileUploadManager fileUploadManager) {
}

@RequestMapping(value = "/file/upload", method = POST)
public ResponseEntity<String> uploadFile(@RequestBody UploadBase64FileModification upload) {
public ResponseEntity<String> uploadFile(@RequestParam(required = false, value = "resizeImage", defaultValue = "false") boolean resizeImage,
@RequestBody UploadBase64FileModification upload) {
try {

if (resizeImage) {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(upload.getFile()));
//resize only if the image is bigger than 500px on one of the side
if(image.getWidth() > 500 || image.getHeight() > 500) {
UploadBase64FileModification resized = new UploadBase64FileModification();
BufferedImage thumbImg = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, 500, 500, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(thumbImg, "png", baos);

resized.setFile(baos.toByteArray());
resized.setAttributes(upload.getAttributes());
resized.setName(upload.getName());
resized.setType("image/png");
upload = resized;
}
}

return ResponseEntity.ok(fileUploadManager.insertFile(upload));
} catch (Exception e) {
return ResponseEntity.badRequest().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
$scope.$applyAsync(function() {
var imageBase64 = e.target.result;
$scope.imageBase64 = imageBase64;
FileUploadService.upload({file : imageBase64.substring(imageBase64.indexOf('base64,') + 7), type : files[0].type, name : files[0].name}).success(function(imageId) {
FileUploadService.uploadImageWithResize({file : imageBase64.substring(imageBase64.indexOf('base64,') + 7), type : files[0].type, name : files[0].name}).success(function(imageId) {
$scope.obj.fileBlobId = imageId;
})
})
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/resources/js/admin/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@
return {
upload : function(file) {
return $http['post']('/admin/api/file/upload', file).error(HttpErrorHandler.handle);
},

uploadImageWithResize: function(file) {
return $http['post']('/admin/api/file/upload?resizeImage=true', file).error(HttpErrorHandler.handle);
}
};
});
Expand Down

0 comments on commit c6a666f

Please sign in to comment.