Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”€ ν”„λ‘œν•„ 이미지 ν™•μž₯자λ₯Ό μ„œλ²„μ—μ„œ 확인할 수 μžˆλ„λ‘ λ³€κ²½ #279

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 64 additions & 59 deletions src/utils/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,76 @@
import { Area } from "react-easy-crop";
import { Area } from 'react-easy-crop';

export const createImage = (url:string) =>
new Promise<HTMLImageElement>((resolve, reject) => {
export const createImage = (url: string) =>
new Promise<HTMLImageElement>((resolve, reject) => {
const image = new Image();
image.addEventListener("load", () => resolve(image));
image.addEventListener("error", (error) => reject(error));
image.setAttribute("crossOrigin", "anonymous");
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', (error) => reject(error));
image.setAttribute('crossOrigin', 'anonymous');
image.src = url;
});
});

export const getRadianAngle = (degreeValue:number) => (degreeValue * Math.PI) / 180;
export const getRadianAngle = (degreeValue: number) =>
(degreeValue * Math.PI) / 180;

export function rotateSize(width: number, height: number, rotation: number) {
const rotRad = getRadianAngle(rotation);
return {
width:
Math.abs(Math.cos(rotRad) * width) + Math.abs(Math.sin(rotRad) * height),
height:
Math.abs(Math.sin(rotRad) * width) + Math.abs(Math.cos(rotRad) * height),
};
const rotRad = getRadianAngle(rotation);
return {
width:
Math.abs(Math.cos(rotRad) * width) + Math.abs(Math.sin(rotRad) * height),
height:
Math.abs(Math.sin(rotRad) * width) + Math.abs(Math.cos(rotRad) * height),
};
}

export async function getCroppedImg(
imageSrc:string,
rotation: 0,
pixelCrop?:Area,
) {
const image = await createImage(imageSrc);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
imageSrc: string,
rotation: 0,
pixelCrop?: Area
) {
const image = await createImage(imageSrc);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

if(!pixelCrop) return null;
if (!ctx) return null;

const rotRad = getRadianAngle(rotation);

const { width: bBoxWidth, height: bBoxHeight } = rotateSize(
image.width,
image.height,
rotation
);

canvas.width = bBoxWidth;
canvas.height = bBoxHeight;

ctx.translate(bBoxWidth / 2, bBoxHeight / 2);
ctx.rotate(rotRad);
ctx.scale(1, 1);
ctx.translate(-image.width / 2, -image.height / 2);

ctx.drawImage(image, 0, 0);

const data = ctx.getImageData(
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height
);

canvas.width = pixelCrop.width;
canvas.height = pixelCrop.height;

ctx.putImageData(data, 0, 0);
if (!pixelCrop) return null;
if (!ctx) return null;

return new Promise<Blob>((resolve, reject) => {
canvas.toBlob((file) => {
if(file) resolve(file);
}, "image/jpeg");
const rotRad = getRadianAngle(rotation);

const { width: bBoxWidth, height: bBoxHeight } = rotateSize(
image.width,
image.height,
rotation
);

canvas.width = bBoxWidth;
canvas.height = bBoxHeight;

ctx.translate(bBoxWidth / 2, bBoxHeight / 2);
ctx.rotate(rotRad);
ctx.scale(1, 1);
ctx.translate(-image.width / 2, -image.height / 2);

ctx.drawImage(image, 0, 0);

const data = ctx.getImageData(
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height
);

canvas.width = pixelCrop.width;
canvas.height = pixelCrop.height;

ctx.putImageData(data, 0, 0);

return new Promise<Blob>((resolve, reject) => {
canvas.toBlob((blob) => {
if (blob) {
const extension = blob.type.split('/')[1];
const file = new File([blob], `profile.${extension}`, { type: blob.type });
return resolve(file);
} else reject(new Error('Canvas is empty'));
});
}
});
}
Loading