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