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

Conditional Resizing using cfs:graphicsmagick

目前,我正在 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() 函数,但我无法让它工作。如何有条件地缩放图像?

提前致谢。

附加

  • '>' 最大 width/height
  • '^' 最小 width/height

    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.