Integration
This guide demonstrates how to integrate Snapkit with various programming languages and frameworks.
Usage Examples
- Below are code samples showing how to optimize and display Snapkit images in different environments.
- The examples use the .avif format for optimized images, but you can also use .webp, .progressive.jpeg, or other available formats.
- The best format for your use case may vary. For more information, see the image formats guide.
Next.js
// Checkout https://nextjs.org/docs/pages/api-reference/components/image#loaderfile
// next.config.js
module.exports = {
images: {
loader: "custom",
loaderFile: "./my/image/loader.js",
},
};
// my/image/loader.js
export default function myImageLoader({ src, width }) {
const url = new URL(
`https://cdn.snapkit.studio/<your-organization>/image${src}`
);
url.searchParams.set("w", width.toString());
return url.href;
}
// Example.tsx
import Image from "next/image";
function Example() {
const src = "/original/HN1QWZh9/806x605/sleepy-dog.jpeg";
const optimizedSrc = src
.replace(/\/original\//, "/gen/")
.replace(/\.[^./]+$/, ".avif");
const placeholderURL = src
.replace(/\/original\//, "/gen/")
.replace(/\.[^./]+$/, ".blurhash.png");
return (
<Image
src={optimizedSrc}
width={806}
height={605}
style={{
backgroundImage: `url('${placeholderURL}')`,
backgroundSize: "cover",
backgroundPosition: "center",
}}
/>
);
}
export default Example;
-
dpr
- dpr stands for device pixel ratio, which represents the ratio of physical pixels to logical pixels on a device.
- Using dpr ensures your images are displayed at the correct resolution for each device.
- If you omit dpr, images may appear blurry or load slowly on high-resolution screens.
-
placeholderURL
placeholderURL
is the URL for a low-quality placeholder image.- This placeholder is displayed while the main image is loading, improving perceived performance and user experience.
- For more details, see the placeholder image documentation.
Note: The logic for generating placeholderURL from the original image URL is provided as an example. You can construct these URLs directly without making additional HTTP requests.
Last updated on