gpt4 book ai didi

javascript - 使用 cfs 有条件调整大小 :graphicsmagick

转载 作者:行者123 更新时间:2023-11-28 19:11:34 26 4
gpt4 key购买 nike

目前,我正在 Amazon S3 上存储图像(个人资料图像),该图像运行良好。我正在使用 cfs:graphicsmagick 将图像大小调整为 300 像素宽,但我只想在图像超过 300 像素时才执行此操作。如果有人上传较小的内容,我不想放大它,这会让它看起来很糟糕。

我当前的代码(无条件)如下:

var profileStore = new FS.Store.S3("profileImages", {
accessKeyId: "--KEY--",
secretAccessKey: "--KEY--",
bucket: "meteor-intrepid",
folder: "profiles",
transformWrite: function(fileObj, readStream, writeStream) {
gm(readStream, fileObj.name()).resize('300').stream().pipe(writeStream);
}
});

如您所见,我在 FS.Store.S3 对象中使用 transformWrite 来处理此问题。我阅读了正在使用的 Node.js 库 (gm) 的文档,我看到有一个 .size() 函数,但我无法让它工作。如何有条件地缩放图像?

提前致谢。

最佳答案

追加

  • '>' 表示最大宽度/高度
  • “^”表示最小宽度/高度

    var profileStore = new FS.Store.S3("profileImages", {
    accessKeyId: "--KEY--",
    secretAccessKey: "--KEY--",
    bucket: "meteor-intrepid",
    folder: "profiles",
    transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize('300>').stream().pipe(writeStream);
    }
    });

摘自 GraphicMagick doc :

By default, the width and height are maximum values. That is, the image is expanded or contracted to fit the width and height value while maintaining the aspect ratio of the image.

Append a ^ to the geometry so that the image is resized while maintaining the aspect ratio of the image, but the resulting width or height are treated as minimum values rather than maximum values.

Append an exclamation point to the geometry to force the image size to exactly the size you specify. For example, if you specify 640x480! the image width is set to 640 pixels and height to 480.

Use > to change the dimensions of the image only if its width or height exceeds the geometry specification. < resizes the image only if both of its dimensions are less than the geometry specification. For example, if you specify '640x480>' and the image size is 256x256, the image size does not change. However, if the image is 512x512 or 1024x1024, it is resized to 480x480. Enclose the geometry specification in quotation marks to prevent the < or > from being interpreted by your shell as a file redirection.

关于javascript - 使用 cfs 有条件调整大小 :graphicsmagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com