/** * 图片预览组件 * 显示图片预览及基本信息,支持错误处理和点击放大 * * 用法: * * * @param {Object} image - 图片数据对象 * @param {boolean} [isInModal=false] - 是否在模态框中显示 * @param {function} [onEnlarge] - 点击放大回调函数 */ import React from 'react'; import DTAI_SITE from './DEFINE.js'; const ImagePreview = ({ image, isInModal = false, onEnlarge }) => { /** * 获取图片预览URL * @param {string} imagePath - 图片路径 * @returns {string} 完整图片URL */ const getImagePreviewUrl = (imagePath) => { if (!imagePath) return ''; return `${DTAI_SITE}${imagePath}`; }; const previewUrl = getImagePreviewUrl(image.imagePath); const imageSizeInfo = image.imageSize ? `大小: ${image.imageSize}` : '大小: 未知'; const resolutionInfo = image.imageWidth && image.imageHeight ? `分辨率: ${image.imageResolution}` : '分辨率: 未知'; return (
{previewUrl ? (
{image.imageName { e.target.onerror = null; e.target.src = './pictures/R-C.jpg'; }} title={`${imageSizeInfo}\n${resolutionInfo}`} onClick={isInModal && onEnlarge ? () => onEnlarge(previewUrl) : undefined} />
{imageSizeInfo}
{resolutionInfo}
) : (
无预览图
)}
); }; export default ImagePreview;