在这里,我发布了我为循环图像所做的解决方案,然后将它们组合在帧图像 (png) 上。该解决方案的唯一缺点是对文件有多次写入,而我无法在一次写入中完成。
我希望这对将来的人有所帮助,
const gm = require('gm').subClass({ imageMagick: true })
export async function convertToCircularImage(imagePath: string, resultPath: string) {
const radius = 180
return new Promise(function (resolve, reject) {
gm(imagePath)
.autoOrient()
.gravity('Center')
.resize(radius, radius, '^')
.extent(radius, radius)
.noProfile()
.setFormat('png')
.out('(')
.rawSize(radius, radius)
.out('xc:Black')
.fill('White')
.drawCircle(radius / 2, radius / 2, radius / 2, 1)
.out('-alpha', 'Copy')
.out(')')
.compose('CopyOpacity')
.out('-composite')
.trim()
.write(resultPath, (err: Error) => {
if (err) {
console.error('Failed to crop image.', err)
reject(err)
} else {
console.log(`Cropped image at ${imagePath} and saved it at ${resultPath}`)
resolve(resultPath)
}
})
})
}
export async function composite(
frameImagePath: string,
circularImagesPaths: string[],
resultImagePath: string,
points: string[],
) {
let frameImage = frameImagePath
let index = 0
for (const circularImagePath of circularImagesPaths) {
const point = points[index]
try {
// this method return the resultImagePath which is then used as a frame for next composition
frameImage = await composeImage(frameImage, circularImagePath, point, resultImagePath)
} catch (e) {
console.log('Composite: some error', e)
}
index = index + 1
}
}
async function composeImage(
frameImage: string,
circularImage: string,
point: string,
resultPath: string,
): Promise<string | any> {
console.log('Composing circular image to frame...', frameImage, circularImage)
return new Promise(function (resolve, reject) {
gm(frameImage)
.composite(circularImage)
.in('-compose', 'Dst_Over') // to only overlap on transparent parts
.geometry(point)
.in()
.write(resultPath, function (err: any) {
if (err) {
console.error('Composing failed', err)
reject(err)
} else {
console.log('Composing complete')
resolve(resultPath)
}
})
})
}