You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.2 KiB
61 lines
2.2 KiB
const fs = require('fs');
|
|
const path = require('path');
|
|
const ImageProcessor = require('./image-processor');
|
|
|
|
// 测试添加水印功能
|
|
async function testAddWatermark() {
|
|
try {
|
|
console.log('开始测试图片水印功能...');
|
|
|
|
// 创建一个简单的测试图片Buffer(使用Base64编码的1x1像素黑色图片)
|
|
const base64TestImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
|
|
|
|
// 处理水印
|
|
const watermarkedBuffer = await ImageProcessor.addWatermarkToBase64(
|
|
base64TestImage,
|
|
'测试水印 - 供应商ID: test123',
|
|
{
|
|
fontSize: 16,
|
|
color: 'rgba(255,0,0,0.8)',
|
|
position: 'bottom-right',
|
|
marginX: 10,
|
|
marginY: 10
|
|
}
|
|
);
|
|
|
|
// 保存测试结果到文件
|
|
const testOutputPath = path.join(__dirname, 'watermark-test-output.png');
|
|
await fs.promises.writeFile(testOutputPath, watermarkedBuffer);
|
|
|
|
console.log(`✅ 水印测试成功! 输出文件已保存至: ${testOutputPath}`);
|
|
console.log('\n测试总结:');
|
|
console.log('- ✅ 成功导入ImageProcessor模块');
|
|
console.log('- ✅ 成功添加文字水印到图片');
|
|
console.log('- ✅ 成功将带水印的图片保存为文件');
|
|
console.log('- ✅ Sharp库功能正常工作');
|
|
console.log('\n提示: 在实际API调用中,水印功能会在创建货源时自动应用到上传的图片上。');
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ 水印测试失败:', error.message);
|
|
console.error('错误详情:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
(async () => {
|
|
console.log('====================================');
|
|
console.log(' 图片水印功能测试脚本 ');
|
|
console.log('====================================');
|
|
|
|
const success = await testAddWatermark();
|
|
|
|
if (success) {
|
|
console.log('\n✅ 所有测试通过! 水印功能已准备就绪,可以在创建货源API中使用。');
|
|
} else {
|
|
console.log('\n❌ 测试失败,请检查错误信息并修复问题。');
|
|
}
|
|
|
|
console.log('====================================');
|
|
})();
|
|
|