-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(user): 사진 규격보다 크면 규격만큼 자르는 기능 개발
- Loading branch information
Showing
7 changed files
with
571 additions
and
14 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
152 changes: 152 additions & 0 deletions
152
apps/user/src/components/form/CropImageModal/CropImageModal.tsx
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,152 @@ | ||
import React, { useState, useCallback } from 'react'; | ||
import { color } from '@maru/design-token'; | ||
import { Button, Column, Text } from '@maru/ui'; | ||
import { flex } from '@maru/utils'; | ||
import styled from 'styled-components'; | ||
import type { Area } from 'react-easy-crop'; | ||
import Cropper from 'react-easy-crop'; | ||
import { getCropImg } from '@/utils'; | ||
|
||
interface Props { | ||
zoom: number; | ||
isOpen: boolean; | ||
imageSrc: string; | ||
onClose: () => void; | ||
onCropComplete: (croppedImage: Blob) => void; | ||
} | ||
|
||
const CropImageModal = ({ zoom, isOpen, imageSrc, onClose, onCropComplete }: Props) => { | ||
const [crop, setCrop] = useState({ x: 0, y: 0 }); | ||
const [cropArea, setCropArea] = useState<Area | null>(null); | ||
const [currentZoom, setCurrentZoom] = useState(zoom); | ||
|
||
const onCropCompleteInternal = useCallback( | ||
(cropArea: Area, croppedAreaPixels: Area) => { | ||
setCropArea(croppedAreaPixels); | ||
}, | ||
[] | ||
); | ||
|
||
const handleApplyCrop = useCallback(async () => { | ||
if (!cropArea) return; | ||
const croppedImage = await getCropImg(imageSrc, cropArea, 117, 156); | ||
|
||
const response = await fetch(croppedImage as string); | ||
const blob = await response.blob(); | ||
|
||
onCropComplete(blob); | ||
onClose(); | ||
}, [imageSrc, cropArea, onCropComplete, onClose]); | ||
|
||
const handleZoomChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setCurrentZoom(Number(e.target.value)); | ||
}; | ||
|
||
if (!isOpen) return null; | ||
|
||
return ( | ||
<BlurBackground> | ||
<ModalContainer> | ||
<Column alignItems="center" gap={16}> | ||
<CropImageModalStyle> | ||
<Cropper | ||
image={imageSrc} | ||
crop={crop} | ||
zoom={currentZoom} | ||
aspect={117 / 156} | ||
onCropChange={setCrop} | ||
onCropComplete={onCropCompleteInternal} | ||
onZoomChange={setCurrentZoom} | ||
style={{ | ||
containerStyle: { width: '100%', height: '100%', position: 'relative' }, | ||
}} | ||
/> | ||
</CropImageModalStyle> | ||
<ZoomBoxStyle> | ||
<InputStyle | ||
type="range" | ||
min="1" | ||
max="3" | ||
step="0.1" | ||
value={currentZoom} | ||
onChange={handleZoomChange} | ||
/> | ||
</ZoomBoxStyle> | ||
<Button onClick={handleApplyCrop}> | ||
<Text fontType="btn1" color={color.white}> | ||
사진 자르기 적용 | ||
</Text> | ||
</Button> | ||
</Column> | ||
</ModalContainer> | ||
</BlurBackground> | ||
); | ||
}; | ||
|
||
export default CropImageModal; | ||
|
||
const BlurBackground = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100vh; | ||
background: rgba(0, 0, 0, 0.4); | ||
z-index: 1000; | ||
`; | ||
|
||
const ModalContainer = styled.div` | ||
margin-top: 10%; | ||
padding: 20px; | ||
border-radius: 10px; | ||
max-width: 468px; | ||
width: 100%; | ||
text-align: center; | ||
height: 357px; | ||
`; | ||
|
||
const CropImageModalStyle = styled.div` | ||
${flex({ flexDirection: 'column', justifyContent: 'center' })} | ||
width: 100%; | ||
height: 400px; | ||
border-radius: 6px; | ||
border: 3px solid ${color.gray300}; | ||
position: relative; | ||
overflow: hidden; | ||
`; | ||
|
||
const ZoomBoxStyle = styled.div` | ||
background-color: ${color.white}; | ||
width: 80%; | ||
height: 60px; | ||
border-radius: 6px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding-left: 20px; | ||
padding-right: 20px; | ||
`; | ||
|
||
const InputStyle = styled.input.attrs({ type: 'range' })` | ||
-webkit-appearance: none; | ||
appearance: none; | ||
width: 100%; | ||
background: ${color.gray300}; | ||
border-radius: 999px; | ||
height: 8px; | ||
&::-webkit-slider-thumb { | ||
-webkit-appearance: none; | ||
appearance: none; | ||
width: 20px; | ||
height: 20px; | ||
background: ${color.white}; | ||
border: 1px solid ${color.maruDefault}; | ||
border-radius: 50%; | ||
position: relative; | ||
cursor: pointer; | ||
box-shadow: 0 0 0 2px ${color.maruLightBlue}; | ||
} | ||
`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const createImage = (url: string): Promise<HTMLImageElement> => | ||
new Promise((resolve, reject) => { | ||
const image = new Image(); | ||
image.addEventListener('load', () => resolve(image)); | ||
image.addEventListener('error', (error) => reject(error)); | ||
image.setAttribute('crossOrigin', 'anonymous'); | ||
image.src = url; | ||
}); | ||
|
||
interface PixelCrop { | ||
x: number; | ||
y: number; | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export const getCropImg = async ( | ||
imageSrc: string, | ||
pixelCrop: PixelCrop, | ||
outputWidth = 117, | ||
outputHeight = 156 | ||
) => { | ||
const image = await createImage(imageSrc); | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
canvas.width = outputWidth * 2; | ||
canvas.height = outputHeight * 2; | ||
|
||
if (!ctx) { | ||
throw new Error('Canvas 2D context not available'); | ||
} | ||
|
||
ctx.imageSmoothingEnabled = true; | ||
ctx.imageSmoothingQuality = 'high'; | ||
|
||
ctx.drawImage( | ||
image, | ||
pixelCrop.x, | ||
pixelCrop.y, | ||
pixelCrop.width, | ||
pixelCrop.height, | ||
0, | ||
0, | ||
outputWidth * 2, | ||
outputHeight * 2 | ||
); | ||
|
||
const downscaledCanvas = document.createElement('canvas'); | ||
const downscaledCtx = downscaledCanvas.getContext('2d'); | ||
|
||
downscaledCanvas.width = outputWidth; | ||
downscaledCanvas.height = outputHeight; | ||
|
||
if (!downscaledCtx) { | ||
throw new Error('Canvas 2D context not available'); | ||
} | ||
|
||
downscaledCtx.drawImage(canvas, 0, 0, outputWidth, outputHeight); | ||
|
||
return new Promise((resolve) => { | ||
downscaledCanvas.toBlob((blob) => { | ||
if (blob) { | ||
resolve(URL.createObjectURL(blob)); | ||
} | ||
}, 'image/jpeg'); | ||
}); | ||
}; |
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
Oops, something went wrong.