-
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.
[Feature/326] 서버에서 블러 이미지 처리하도록 수정 (#431)
* feat: 블러 이미지 등록 구현 * feat: 기존 유저들 blurImage 등록하는 API 구현 * feat: 코드 깨지는 것 수정 * feat: 오타 수정 * feat: id 필터
- Loading branch information
1 parent
ba39ab3
commit 2cfa9d0
Showing
12 changed files
with
188 additions
and
7 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
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
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
30 changes: 30 additions & 0 deletions
30
app/src/main/kotlin/com/nexters/bottles/app/common/component/ImageProcessor.kt
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,30 @@ | ||
package com.nexters.bottles.app.common.component | ||
|
||
import org.springframework.stereotype.Component | ||
import java.awt.image.BufferedImage | ||
import java.awt.image.ConvolveOp | ||
import java.awt.image.Kernel | ||
import java.io.File | ||
import javax.imageio.ImageIO | ||
import kotlin.math.sqrt | ||
|
||
@Component | ||
class ImageProcessor { | ||
|
||
fun blurImage(file: File): BufferedImage { | ||
val image = ImageIO.read(file) | ||
val blurredImage = applyGaussianBlur(image) | ||
return blurredImage | ||
} | ||
|
||
private fun applyGaussianBlur(image: BufferedImage): BufferedImage { | ||
val matrix = FloatArray(1225) { 1 / 1225f } | ||
val size = sqrt(matrix.size.toDouble()).toInt() | ||
val kernel = Kernel(size, size, matrix) | ||
val convolveOp = ConvolveOp(kernel, ConvolveOp.EDGE_ZERO_FILL, null) | ||
|
||
val blurredImage = BufferedImage(image.width, image.height, image.type) | ||
convolveOp.filter(image, blurredImage) | ||
return blurredImage | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/kotlin/com/nexters/bottles/app/common/component/ImageUploader.kt
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,43 @@ | ||
package com.nexters.bottles.app.common.component | ||
|
||
import org.springframework.stereotype.Component | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.io.File | ||
import java.net.URL | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.time.LocalDateTime | ||
import javax.imageio.ImageIO | ||
|
||
@Component | ||
class ImageUploader( | ||
private val imageProcessor: ImageProcessor, | ||
private val fileService: FileService | ||
) { | ||
|
||
fun upload(file: MultipartFile, path: String): URL { | ||
return fileService.upload(file, path) | ||
} | ||
|
||
fun uploadWithBlur(file: MultipartFile, path: String): URL { | ||
val uploadDir = Paths.get("uploads") | ||
if (!Files.exists(uploadDir)) { | ||
Files.createDirectories(uploadDir) | ||
} | ||
|
||
val originalFilePath = uploadDir.resolve("original_" + LocalDateTime.now() + file.originalFilename) | ||
Files.copy(file.inputStream, originalFilePath) | ||
|
||
val blurredImage = imageProcessor.blurImage(File(originalFilePath.toString())) | ||
|
||
val blurredFilePath = uploadDir.resolve("blurred_" + LocalDateTime.now() + file.originalFilename) | ||
ImageIO.write(blurredImage, "jpg", File(blurredFilePath.toString())) | ||
|
||
val imageUrlBlur = fileService.upload(blurredFilePath.toString(), "blurred_${path}") | ||
|
||
Files.deleteIfExists(originalFilePath) | ||
Files.deleteIfExists(blurredFilePath) | ||
|
||
return imageUrlBlur | ||
} | ||
} |
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
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
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