Skip to content

Commit

Permalink
画像ファイルタイプの検証を追加し、無効なファイルタイプの場合にエラーメッセージを返すように修正しました。また、画像のクロスオリジン設定を追…
Browse files Browse the repository at this point in the history
…加しました。
  • Loading branch information
ttizze committed Mar 4, 2025
1 parent f520568 commit 61fd216
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,31 @@ async function getImageDimensions(
file: File,
): Promise<{ width: number; height: number }> {
return new Promise((resolve, reject) => {
if (!file.type.startsWith("image/")) {
reject(new Error("無効なファイルタイプです。画像のみ許可されています。"));
return;
}

const blobUrl = URL.createObjectURL(file);
const img = new Image();

img.onload = () => {
resolve({ width: img.naturalWidth, height: img.naturalHeight });
const dimensions = {
width: img.naturalWidth,
height: img.naturalHeight,
};

URL.revokeObjectURL(blobUrl);
resolve(dimensions);
};

img.onerror = (err) => {
URL.revokeObjectURL(blobUrl);
reject(err);
};

img.crossOrigin = "anonymous";

img.src = blobUrl;
});
}

0 comments on commit 61fd216

Please sign in to comment.