Skip to content

Commit

Permalink
Feat: 복사 짤 util 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjinan096 committed Mar 5, 2024
1 parent 75ef2e4 commit 98319ec
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/utils/copyZzal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { toast } from "react-toastify";
import axios from "axios";

const createImage = (src: string) => {
const image = document.createElement("img");
image.src = src;
return image;
};

const copyToClipboard = async (pngBlob: Blob) => {
try {
await navigator.clipboard.write([
new ClipboardItem({
[pngBlob.type]: pngBlob,
}),
]);
toast.success("성공적으로 복사되었습니다.");
} catch (error) {
toast.error("복사에 실패했습니다.");
console.error(error);
}
};

const copyJpgToClipboard = (imgBlob: Blob) => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const image = createImage(window.URL.createObjectURL(imgBlob));
image.onload = (event) => {
if (event && event.target) {
const target = event.target as HTMLImageElement;
canvas.width = target.width;
canvas.height = target.height;
if (ctx) {
ctx.drawImage(target, 0, 0, target.width, target.height);
canvas.toBlob(
(blob) => {
if (blob) {
copyToClipboard(blob);
}
},
"image/png",
1,
);
}
}
};
};

const copyZzal = async (imageUrl: string) => {
try {
const imgResponse = await axios.get(imageUrl, { responseType: "blob" });
const imgBlob = imgResponse.data;
const extension = imageUrl.split(".").pop()?.toLowerCase();

if (extension === "jpg") {
copyJpgToClipboard(imgBlob);
} else {
console.error("예상치 못한 파일 형식입니다: " + extension);
}
} catch (error) {
toast.error("복사에 실패했습니다.");
console.error(error);
}
};

export default copyZzal;

0 comments on commit 98319ec

Please sign in to comment.