0

Currently, I'm storing images (profile images) on Amazon S3, which is working perfectly. I'm resizing images to be 300px wide using cfs:graphicsmagick, but I only want to be doing this if they're wider than 300px. If someone's uploading something smaller, I don't want to be scaling it up, which would make it look awful.

My current code (not conditional) is as follows:

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

As you can see, I'm handling this using a transformWrite in my FS.Store.S3 object. I read the documentation for the Node.js library that's in use (gm), and I see that there's a .size() function, but I wasn't able to get it working. How can images be scaled conditionally?

Thanks in advance.

Lieuwe Rooijakkers
  • 164
  • 1
  • 4
  • 14
MisutoWolf
  • 1,133
  • 1
  • 18
  • 33
  • http://stackoverflow.com/questions/12570834/how-to-preview-image-get-file-size-image-height-and-width-before-upload - This might be helpful. Not using gm, but instead native image handling. – below9k Jun 04 '15 at 15:18

1 Answers1

3

Append

  • '>' for maximum width/height
  • '^' for minimum 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);
      }
    });
    

Excerpt from 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.

Firdaus Ramlan
  • 1,146
  • 8
  • 11