-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
420 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { getImageUrlBuilder } from "#app/utils/image"; | ||
|
||
interface CloudinaryImageProps | ||
extends Omit< | ||
React.DetailedHTMLProps< | ||
React.ImgHTMLAttributes<HTMLImageElement>, | ||
HTMLImageElement | ||
>, | ||
"src" | "srcSet" | "sizes" | ||
> { | ||
id: string; | ||
widths: number[]; | ||
sizes: string[]; | ||
} | ||
export function CloudinaryImage({ | ||
id, | ||
widths, | ||
sizes, | ||
alt, | ||
...imgProps | ||
}: CloudinaryImageProps) { | ||
const buildImageUrl = getImageUrlBuilder(id); | ||
const averageWidth = Math.ceil( | ||
widths.reduce((a, s) => a + s) / widths.length, | ||
); | ||
|
||
const src = buildImageUrl({ | ||
transformations: { | ||
resize: { | ||
width: averageWidth, | ||
}, | ||
}, | ||
}); | ||
|
||
const srcSet = widths | ||
.map((width) => | ||
[ | ||
buildImageUrl({ | ||
transformations: { | ||
resize: { | ||
width: width, | ||
}, | ||
}, | ||
}), | ||
`${width}w`, | ||
].join(" "), | ||
) | ||
.join(", "); | ||
|
||
return ( | ||
<img | ||
{...imgProps} | ||
alt={alt} | ||
src={src} | ||
srcSet={srcSet} | ||
sizes={sizes.join(", ")} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { | ||
buildImageUrl as _buildImageUrl, | ||
setConfig, | ||
} from "cloudinary-build-url"; | ||
import type { CldOptions } from "@cld-apis/types"; | ||
|
||
setConfig({ | ||
cloudName: "nichtsam", | ||
}); | ||
|
||
export function getImageUrlBuilder(id: string) { | ||
return (cldOptions: CldOptions = {}) => _buildImageUrl(id, cldOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.