将GraphicsMagic与生成器函数一起使用并产生收益

时间:2018-06-20 18:34:11

标签: javascript node.js ecmascript-6 yield graphicsmagick

我正在NodeJs中使用GraphicsMagic在png图像中生成水印,然后我想上传到Aws S3服务器,我有两个功能,第一个在图片中创建水印

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}
    );
}

第二个功能是在一个月前为另一个开发人员创建的,我只创建了第一个功能(在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: 对不起,我的英语,我的主要语言是西班牙语

0 个答案:

没有答案