首页 > 解决方案 > 将 GraphicsMagic 与生成器功能和产量一起使用

问题描述

我在 NodeJs 中使用 GraphicsMagic 在 png 图像中生成水印,然后我想上传到 Aws S3 服务器,我有 2 个函数首先在图片中创建水印

function * createWatermark(watermark, text) {
    const textWidth = pixelWidth(text, { size: 37 });

    return gm('public/watermark/' + watermark)
    .size(function (err, size) {
        if (!err) {
            const width = size.width;
            const height = size.height;
            const marginBottom = 30;
            const marginRight = 60;

            //create first pattern
            return gm('public/watermark/' + watermark)
            .font("public/fonts/sofia/2E827A_2_0.ttf", 37)
            .fill('rgba(255,255,255, 0.8)')
            .drawText((width-textWidth-marginRight), (height-marginBottom), text)
            .write('public/img/temp/' + watermark, function(e){
                // console.log('done', e); // What would you like to do here?
                return e;
            });
        }
    });
}

第二个功能是将图片上传到AWS S3服务器

function * uploadCreatedWatermark(fiileName, author, withPattern, format){
    var pathFile = 'public/img/temp/' + fiileName;
    var watermarkReadStream = fs.createReadStream(pathFile);

    yield AWSService.uploadFormFileStreamToAWS(
        Env.get('AWS_WATERMARK_FOLDER') + '/' + format,
        author + (withPattern ? '_pattern' : '')+'.png',
        watermarkReadStream,
        {isRootPath: true}
    );
}

第二个函数是 1 个月前为另一个开发人员创建的,我只创建了第一个函数(在 png 文件中创建水印)。

从主函数我调用两者

class ExampleController {
    * foo(request, response) {
        var text = 'aaron  |  gallereplay';
        var author = 'aaron';

        var watermark = 'Cinemagraph_watermarks_1_1.png';
        yield createWatermark(watermark, text);
        yield uploadCreatedWatermark(watermark, author, false, '1_1');
    }
}

但它失败了,因为第一个函数在第二个函数结束之前运行,并且 png 文件还不存在

额外信息: 1)我的第一个想法是将第二个函数放入第一个函数中,但由于该函数不存在而失败

    function * createWatermark(watermark, text) {
    const textWidth = pixelWidth(text, { size: 37 });

    return gm('public/watermark/' + watermark)
    .size(function (err, size) {
        if (!err) {
            const width = size.width;
            const height = size.height;
            const marginBottom = 30;
            const marginRight = 60;

            //create first pattern
            return gm('public/watermark/' + watermark)
            .font("public/fonts/sofia/2E827A_2_0.ttf", 37)
            .fill('rgba(255,255,255, 0.8)')
            .drawText((width-textWidth-marginRight), (height-marginBottom), text)
            .write('public/img/temp/' + watermark, function(e){
                // console.log('done', e); // What would you like to do here?
                // return e;
                yield uploadCreatedWatermark(watermark, 'author', true, '1_1');
            });
        }
    });
}

2) createWatermark 和uploadCreatedWatermark 两个函数都在类外

class ExampleController() {
    * foo(request, response) { ... }
}
function * createWatermark() { ... }
function * uploadCreatedWatermark() { ... }

PD:对不起我的英语,我的主要语言是西班牙语

标签: javascriptnode.jsecmascript-6yieldgraphicsmagick

解决方案


推荐阅读