Skip to content

Commit

Permalink
Feat: 업로드 이미지 리사이징 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Taekgil99 committed Dec 1, 2022
1 parent 7d0cd60 commit 6bf761a
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
import com.backend.domain.product.exception.NoImage;
import com.backend.domain.product.exception.UploadFailed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.imgscalr.Scalr;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.io.File;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -26,11 +31,19 @@ public class AwsS3Service implements ImageUploadService{
@Value("${cloud.aws.s3.bucket}")
private String bucketName;

@SneakyThrows
public String StoreImage(MultipartFile img) {
validateFileExists(img);
String originalFilename = img.getOriginalFilename();
String storeFileName = createStoreFileName(originalFilename);

//mutipartfile->bufferedImage
BufferedImage bi= ImageIO.read(img.getInputStream());
//이미지 사이즈변경
bi=resizeImage(bi);
//이미지 위치에 저장
ImageIO.write(bi,"jpg",new File(storeFileName));

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(img.getContentType());

Expand All @@ -45,6 +58,10 @@ public String StoreImage(MultipartFile img) {
return amazonS3.getUrl(bucketName, storeFileName).toString();
}

private BufferedImage resizeImage(BufferedImage src) {
return Scalr.resize(src, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_EXACT, 500, 500, Scalr.OP_ANTIALIAS);
}

public void deleteImage(String fileUrl) {
String splitStr = ".com/";
String fileName = fileUrl.substring(fileUrl.lastIndexOf(splitStr) + splitStr.length());
Expand Down

0 comments on commit 6bf761a

Please sign in to comment.