-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from AppLinkers/develop
Deploy for thumbnail Image
- Loading branch information
Showing
6 changed files
with
110 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/laser/ordermanage/common/util/ImageUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.laser.ordermanage.common.util; | ||
|
||
import com.laser.ordermanage.common.exception.CommonErrorCode; | ||
import com.laser.ordermanage.common.exception.CustomCommonException; | ||
import org.imgscalr.Scalr; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class ImageUtil { | ||
|
||
private ImageUtil() { | ||
|
||
} | ||
|
||
public static String extractThumbnail(MultipartFile multipartFile, String tempFolderPath) { | ||
try { | ||
// MultipartFile to BufferedImage | ||
BufferedImage bufferedImage = ImageIO.read(multipartFile.getInputStream()); | ||
|
||
// resize image | ||
BufferedImage resizedImage = Scalr.resize(bufferedImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_EXACT, 500, 500, Scalr.OP_ANTIALIAS); | ||
|
||
String filePath = tempFolderPath + FileUtil.getFileNameWithoutExtension(multipartFile) + "-thumbnail.png"; | ||
|
||
ImageIO.write(resizedImage, "png", new File(filePath)); | ||
|
||
return filePath; | ||
} catch (IOException e) { | ||
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_EXTRACT_THUMBNAIL); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/laser/ordermanage/common/util/PDFUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.laser.ordermanage.common.util; | ||
|
||
import com.laser.ordermanage.common.exception.CommonErrorCode; | ||
import com.laser.ordermanage.common.exception.CustomCommonException; | ||
import org.apache.pdfbox.Loader; | ||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.rendering.PDFRenderer; | ||
import org.imgscalr.Scalr; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
public class PDFUtil { | ||
|
||
private PDFUtil() { | ||
|
||
} | ||
|
||
public static String extractThumbnail(MultipartFile multipartFile, String tempFolderPath) { | ||
try { | ||
// 일시적인 파일 생성 | ||
Path tempFile = Files.createTempFile("uploadedDrawing-", ".pdf"); | ||
Files.copy(multipartFile.getInputStream(), tempFile, StandardCopyOption.REPLACE_EXISTING); | ||
|
||
try (PDDocument document = Loader.loadPDF(tempFile.toFile())) { | ||
PDFRenderer renderer = new PDFRenderer(document); | ||
BufferedImage originalImage = renderer.renderImageWithDPI(0, 300); | ||
|
||
// 이미지 크기 조정 | ||
BufferedImage resizedImage = Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_EXACT, 500, 500, Scalr.OP_ANTIALIAS); | ||
|
||
String filePath = tempFolderPath + FileUtil.getFileNameWithoutExtension(multipartFile) + "-thumbnail.png"; | ||
ImageIO.write(resizedImage, "png", new File(filePath)); | ||
|
||
// 임시 파일 삭제 | ||
Files.deleteIfExists(tempFile); | ||
|
||
return filePath; | ||
} | ||
} catch (IOException e) { | ||
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_EXTRACT_THUMBNAIL); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters