( dims: ImageDimensions, sourcePath?: string, )
| 833 | * Returns null if no useful metadata is available. |
| 834 | */ |
| 835 | export function createImageMetadataText( |
| 836 | dims: ImageDimensions, |
| 837 | sourcePath?: string, |
| 838 | ): string | null { |
| 839 | const { originalWidth, originalHeight, displayWidth, displayHeight } = dims |
| 840 | // Skip if dimensions are not available or invalid |
| 841 | // Note: checks for undefined/null and zero to prevent division by zero |
| 842 | if ( |
| 843 | !originalWidth || |
| 844 | !originalHeight || |
| 845 | !displayWidth || |
| 846 | !displayHeight || |
| 847 | displayWidth <= 0 || |
| 848 | displayHeight <= 0 |
| 849 | ) { |
| 850 | // If we have a source path but no valid dimensions, still return source info |
| 851 | if (sourcePath) { |
| 852 | return `[Image source: ${sourcePath}]` |
| 853 | } |
| 854 | return null |
| 855 | } |
| 856 | // Check if image was resized |
| 857 | const wasResized = |
| 858 | originalWidth !== displayWidth || originalHeight !== displayHeight |
| 859 | |
| 860 | // Only include metadata if there's useful info (resized or has source path) |
| 861 | if (!wasResized && !sourcePath) { |
| 862 | return null |
| 863 | } |
| 864 | |
| 865 | // Build metadata parts |
| 866 | const parts: string[] = [] |
| 867 | |
| 868 | if (sourcePath) { |
| 869 | parts.push(`source: ${sourcePath}`) |
| 870 | } |
| 871 | |
| 872 | if (wasResized) { |
| 873 | const scaleFactor = originalWidth / displayWidth |
| 874 | parts.push( |
| 875 | `original ${originalWidth}x${originalHeight}, displayed at ${displayWidth}x${displayHeight}. Multiply coordinates by ${scaleFactor.toFixed(2)} to map to original image.`, |
| 876 | ) |
| 877 | } |
| 878 | |
| 879 | return `[Image: ${parts.join(', ')}]` |
| 880 | } |
| 881 |
no test coverage detected