All files / src/utils images.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
const defaultPicture = "/images/blog/M795H8A.jpg";
 
export enum SizeMapping {
  smallSquare = "s",
  bigSquare = "b",
  small = "t",
  medium = "m",
  large = "l",
  huge = "h",
}
 
/**
 * Parse image path and return the appropriate size variant.
 * Works with local paths like '/images/blog/ABC123.png'
 *
 * For large size: returns '{id}-640.{ext}' (640px width)
 * For other sizes or GIF: returns original path unchanged
 * GIF images are never resized.
 */
export const parseImage = (
  rawImage: string,
  size: SizeMapping = SizeMapping.large,
): string => {
  if (!rawImage) {
    return defaultPicture;
  }
 
  // Don't resize GIF images
  if (rawImage.match(/\.gif$/i)) {
    return rawImage;
  }
 
  // For large size, return 640px variant
  if (size === SizeMapping.large) {
    return rawImage.replace(/\.([^.]+)$/, "-640.$1");
  }
 
  // For other sizes, return original (local hosting doesn't need multiple sizes)
  return rawImage;
};
 
/**
 * Get WebP URL for a given image path.
 * Returns the .webp version of .png/.jpg/.jpeg images.
 * GIF images return the original path.
 */
export const getWebPUrl = (imagePath: string): string | null => {
  if (!imagePath || imagePath.match(/\.gif$/i)) {
    return null;
  }
  return imagePath.replace(/\.(png|jpe?g)$/i, ".webp");
};
 
// Backward compatibility alias
export const parseImgur = parseImage;