export async function getImageInfo(imageUrl: string) {
	try {
		// 获取文件大小
		const sizeResponse = await fetch(imageUrl, { method: 'HEAD' });
		const contentLength = sizeResponse.headers.get('Content-Length');

		// 获取图片尺寸
		const dimensions = await new Promise((resolve, reject) => {
			const img = new Image();
			img.onload = () =>
				resolve({
					width: img.naturalWidth,
					height: img.naturalHeight,
				});
			img.onerror = reject;
			img.src = imageUrl;
		});

		// 格式化大小
		const formatSize = (bytes: number) => {
			if (!bytes) return '未知';
			const k = 1024;
			const sizes = ['Bytes', 'KB', 'MB', 'GB'];
			const i = Math.floor(Math.log(bytes) / Math.log(k));
			return (
				parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
			);
		};

		return {
			dimensions,
			size: {
				bytes: contentLength ? parseInt(contentLength, 10) : null,
				formatted: contentLength
					? formatSize(parseInt(contentLength, 10))
					: '未知',
			},
			url: imageUrl,
		};
	} catch (error) {
		console.error('获取图片信息失败:', error);
		return null;
	}
}
