javascript - 将 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:对不起我的英语,我的主要语言是西班牙语
解决方案
推荐阅读
- firebase - Ionic / Firebase 应用程序在后台后建立连接缓慢
- php - 我可以在删除所述类别时将图像的 category_id 更改为 null 吗?
- css - 如何理解 MaterialUI 样式的组件?
- java - 使用带有 API 密钥而不是 OAuth 的 Google Sheets Java API?
- jupyter - 执行一段时间后,Jupyter 总是不小心显示“kernel Restarting”
- python - 如何重新索引月份和年份列以插入丢失的数据?
- java - Java:我制作了一个程序,它接受整数数组的输入并在表中显示值。尝试使用字符串数组重新创建它。请。帮助,
- c++ - 有没有办法获得给定数量的输入,其中数字由 C++ 编译时的模板给出?
- javascript - 为什么要在 app.module.ts 中导入 { HttpClientModule }
- php - PHP-MySQL:多文件上传代码中的第一个文件未上传到服务器