You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Google Cloud Run与sharp使生成图片显示文字而非方框?

Node.js + Sharp 文字合成图片在Cloud Run部署后字体显示异常问题

我使用Node.js与sharp构建了一个将文字合成到背景图的服务,核心代码如下:

exports.social_get_result = async function(req,res,next){
    // Path to the existing background image
    const backgroundImagePath = './static/images/bg/social-'+ req.params.division.replace(/([\s]{1,})/g,'-') +'.png';

    // Text settings
    const linesOfText = ["Result: "+ req.params.homeTeam +" vs ", req.params.awayTeam,req.params.homeScore +"-"+ req.params.awayScore,"#tameside #badminton #tbl #result","https://tameside-badminton.co.uk"];
    const textSize = 60;
    const textColor = { r: 0, g: 0, b: 0, alpha: 1 }; // 黑色
    const lineHeight = 1.2; // 行高倍数
    const image = sharp(backgroundImagePath);

    // 获取图片元数据(如尺寸)
    const metadata = await image.metadata();
    const width = metadata.width;
    const height = metadata.height;

    // 创建包含多行文字的SVG叠加层
   
    const svgText = `
        <svg width="${width}" height="${height}">
        <style>
            .title { fill: rgba(${textColor.r}, ${textColor.g}, ${textColor.b}, ${textColor.alpha}); font-size: ${textSize}px; font-weight: bold; font-family:Arial }
            .footer { fill: rgba(${textColor.r}, ${textColor.g}, ${textColor.b}, ${textColor.alpha}); font-size: 30px; font-weight: bold; font-family:Arial }
        </style>
            <text x="10%" y="70%" text-anchor="left">
                <tspan x="10%" dy="1.2em" class="title">${linesOfText[0]}</tspan>
                <tspan x="10%" dy="1.2em" class="title">${linesOfText[1]}</tspan>
                <tspan x="10%" dy="1.2em" class="title">${linesOfText[2]}</tspan>
                <tspan x="10%" dy="1.2em" class="footer">${linesOfText[3]}</tspan>
                <tspan x="10%" dy="1.2em" class="footer">${linesOfText[4]}</tspan>
            </text>
        </svg>`;

    const buffer = Buffer.from(svgText);

    // 将文字合成到背景图
    const finalImage = await image
        .composite([{ input: buffer }])
        .toFormat('JPG')
        .toFile('static/images/generated/'+ req.params.homeTeam.replace(/([\s]{1,})/g,'-') + req.params.awayTeam.replace(/([\s]{1,})/g,'-') +'.jpg')
    
    const outputImage = await image
    .composite([{ input: buffer }])
    .toBuffer()
    .then(data => res.type('png').send(data))
}

部署到Google Cloud Run后,生成的图片中文字显示为方框占位符,本地运行时文字显示正常。日志中出现如下错误:

Fontconfig error: Cannot load default config file: No such file: (null)

我已尝试以下方案,但均未解决问题:

  • 在SVG中使用不同字体(Arial、sans-serif、DejaVu Sans等)
  • 将Arial.ttf及fonts.conf放置在多个位置(服务根目录、fonts文件夹、rootfiles文件夹等)
  • 设置FC_DEBUG参数(如1)
  • 配置FONTCONFIG_PATH和FONTCONFIG_FILE路径(如/usr/share/fonts、/fonts、~/fonts/)
  • 设置环境变量PANGOCAIRO_BACKEND = fontconfig
  • 执行fc-list、fc-cat、fc-cache命令,确认DejaVu Sans等字体存在

我清楚无服务器环境需要特定配置,但始终无法让服务找到可用字体文件或fonts.conf,恳请解决该问题。

内容的提问来源于stack exchange,提问作者Neil Cooper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:12:06