Skip to content

Instantly share code, notes, and snippets.

@yoavniran
Last active September 9, 2019 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoavniran/76b3ad93d43260b739089b68ba7e4aa6 to your computer and use it in GitHub Desktop.
Save yoavniran/76b3ad93d43260b739089b68ba7e4aa6 to your computer and use it in GitHub Desktop.
utility to generate breakpoints with cloudinary
import { generateImageResponsiveAttributes } from "cloudinary/lib/utils/srcsetUtils";
const CLOUD = "__cloud name__";
const BREAK_POINTS = 4;
export const getResponsiveAttributes = (id, min, max, transformation) => {
const sizes = new Array(BREAK_POINTS)
.fill(null)
.map((n, i) => `(max-width: ${min * (i + 1)}px) ${(min * (i + 1)) / 2}px`);
const srcset = generateImageResponsiveAttributes(id, {}, {
min_width: min,
max_width: max,
max_images: BREAK_POINTS,
transformation: { transformation },
}, {
cloud_name: CLOUD,
});
return {
...srcset,
sizes,
};
};
import {getResponsiveAttributes} from "../getResponsiveAttributes";
const Image = ({publidId}) => {
const transformation = [{
quality: "auto",
fetchFormat: "auto",
dpr: 2,
},];
const respAttrs = getResponsiveAttributes(
publicId,
400,
1000,
transformation);
const url = cloudinary.url(publicId, { transformation });
return (
<img src={url}
alt={publicId}
srcSet={respAttrs.srcset}
sizes={respAttrs.sizes}/>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment