Skip to content

Commit

Permalink
feat: img 리사이징 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
hyojoonm committed Dec 1, 2022
1 parent 7852e3e commit 56dcbe8
Showing 1 changed file with 4 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String StoreImage(MultipartFile img) {
String fileName = createFileName(img.getOriginalFilename());
String fileFormatName = img.getContentType().substring(img.getContentType().lastIndexOf("/") + 1);

MultipartFile resizedFile = resizeImage(fileName, fileFormatName, img, 768);
MultipartFile resizedFile = resizeImage(fileName, fileFormatName, img, 500);

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(img.getContentType());
Expand Down Expand Up @@ -78,19 +78,19 @@ MultipartFile resizeImage(String fileName, String fileFormatName, MultipartFile
int originHeight = image.getHeight();

// origin 이미지가 resizing될 사이즈보다 작을 경우 resizing 작업 안 함
if(originWidth < targetWidth)
return originalImage;

MarvinImage imageMarvin = new MarvinImage(image);

Scale scale = new Scale();
scale.load();
scale.setAttribute("newWidth", targetWidth);
scale.setAttribute("newHeight", targetWidth * originHeight / originWidth);
scale.setAttribute("newHeight", targetWidth );
scale.process(imageMarvin.clone(), imageMarvin, null, null, false);

BufferedImage imageNoAlpha = imageMarvin.getBufferedImageNoAlpha();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imageNoAlpha, fileFormatName, baos);
baos.flush();

return new MockMultipartFile(fileName, baos.toByteArray());

Expand All @@ -113,14 +113,4 @@ private void validateFileExists(MultipartFile multipartFile) {
}
}

private static String createStoreFileName(String originalFilename) {
String ext = extractExt(originalFilename);
String uuid = UUID.randomUUID().toString();
return uuid + "." + ext;
}

private static String extractExt(String originalFilename) {
int pos = originalFilename.lastIndexOf(".");
return originalFilename.substring(pos + 1);
}
}

0 comments on commit 56dcbe8

Please sign in to comment.