我一直在使用 CollectionFS 和GraphicsMagick在 S3 中裁剪和存储图片。我意识到图像对于移动来说太大了,现在我想存储缩小版本。这是我尝试过的。
images = new FS.Collection("images", {
stores: [
new Store("images", imageOptions),
new Store("thumbs", thumbOptions) // added later
]
});
thumbs
商店适用于新图片,但缩小旧版图片的启动功能却没有。
Meteor.startup(function () {
images.find().forEach(function (fileObj) {
if (!fileObj.copies.thumbs) {
var readStream = fileObj.createReadStream('images');
var writeStream = fileObj.createWriteStream('thumbs');
gm(readStream)
.scale(THUMB_DIM, THUMB_DIM)
.stream()
.pipe(writeStream);
}
});
});
有什么想法吗?可能是readstream
来自 S3 ?
答案 0 :(得分:1)
好像你缺少gm命令中的第二个参数,它应该是原始文件名。我们使用以下迁移脚本在新商店中创建图像的副本:
Migrations.add({
version: 2,
name: 'Save old images to new image stores',
up: function() {
console.log('Save old images to new image stores');
Images.find().fetch().forEach(function(image) {
if (image.copies['32x32'].size === 0) {
console.log('creating 32x32 image: ' + image.name());
var readStream = image.createReadStream('original');
var writeStream = image.createWriteStream('32x32');
try {
gm(readStream, image.name()).resize(32, 32).stream().pipe(writeStream);
}
catch (e) {
console.log(e);
}
}
if (image.copies['80x80'].size === 0) {
console.log('creating 80x80 image: ' + image.name());
var readStream = image.createReadStream('original');
var writeStream = image.createWriteStream('80x80');
try {
gm(readStream, image.name()).resize(80, 80).stream().pipe(writeStream);
}
catch (e) {
console.log(e);
}
}
});
},
down: function() {
}
});