weddingsite/lib/buildBase64Data.js

44 lines
1,006 B
JavaScript
Raw Permalink Normal View History

2022-01-28 04:59:21 +00:00
import { getPlaiceholder } from 'plaiceholder';
import { buildUrl } from 'cloudinary-build-url';
export default async function buildBase64Data(
cloudinaryUrl,
imageSource,
2022-01-28 04:59:21 +00:00
alt,
2022-02-02 06:08:28 +00:00
additionalProps = {},
transformations = {}
2022-01-28 04:59:21 +00:00
) {
let imagePath = imageSource;
if (cloudinaryUrl) {
const folderName = process.env.PUBLIC_FOLDER_NAME;
const cloudName = process.env.PUBLIC_CLOUD_NAME;
2022-01-28 04:59:21 +00:00
if (imageSource && alt && additionalProps) {
imagePath = buildUrl(`${folderName}/${imageSource}`, {
cloud: {
cloudName,
},
2022-02-02 06:08:28 +00:00
transformations,
});
}
2022-01-28 04:59:21 +00:00
}
if (imagePath) {
try {
const { base64, img } = await getPlaiceholder(imagePath, { size: 10 });
return {
imageProps: {
...img,
blurDataURL: base64,
},
alt,
...additionalProps,
};
} catch (e) {
// Error getting plaiceholder
// throw new Error('Error creating plaiceholder base64 image');
}
}
return {};
}