Files
integration/website/bin/transform-source-backgrounds.mjs
Emmanuel Pelletier e083128b05 website backgrounds: make it easy to force a bg for a service
rework a bit the backgrounds transformation script so that we can later
easily force a specific background for a specific service. This comes
from a request from france-transfert but they changed their mind in the
end. Felt like the logic is good to keep for later though.
2024-05-24 11:02:04 +02:00

51 lines
1.6 KiB
JavaScript

import fs from "fs"
import path from "path"
import { promisify } from "util"
import sharp from "sharp"
import svgGradient from "svg-gradient"
const readdir = promisify(fs.readdir)
const sourcesDir = path.join(import.meta.dirname, "..", "src", "assets", "backgrounds", "sources")
const outputDir = path.join(import.meta.dirname, "..", "src", "assets", "backgrounds")
async function resizeSourceBackgrounds() {
try {
console.log(`Resizing source background files…`)
const gradient = await sharp(
Buffer.from(
svgGradient(`linear-gradient(135deg, rgba(0, 0, 145, 0.6) 0%, rgba(225, 0, 15, 0.6) 100%)`),
),
)
.resize(1920, 1200, { fit: "cover" })
.toBuffer()
const backgrounds = await readdir(sourcesDir)
backgrounds.forEach((backgroundFile, i) => {
const srcPath = path.join(sourcesDir, backgroundFile)
const backgroundName = path.parse(backgroundFile).name
const jpegPath = path.join(outputDir, `${backgroundName}.jpg`)
const avifPath = path.join(outputDir, `${backgroundName}.avif`)
const image = sharp(srcPath)
.resize(1920, 1200, { fit: "cover" })
.composite([{ input: gradient }])
image.toFile(jpegPath).then(() => {
console.log(`Resized ${getFilename(backgroundFile)} to ${getFilename(jpegPath)}`)
})
image.toFile(avifPath).then(() => {
console.log(`Resized ${getFilename(backgroundFile)} to ${getFilename(avifPath)}`)
})
})
} catch (error) {
console.error("Error:", error)
}
}
function getFilename(path) {
return path.split("/").pop()
}
resizeSourceBackgrounds()