title: How get image width to be available for transformWrite
body: <p>I'm trying to set up a FS.store with transformWrite, that scales uploaded images. I do have a max width size. But the scaling should be by integer factor only.</p>
  
  <p>For example, if I have a max width of 100px, and an original image width of 40px, the image should be scaled by an integer factor of 2, which results in a target width of 80px (a factor 3x would result in an image too wide).</p>
  
  <p>To calculate the original width and do the math I've set up the function <code>scaleToMaxByInteger</code> that should return the optimal width. For some reason it is not working in the gm transformation. Why?</p>
  
  <pre><code>var ScaledStore = new FS.Store.FileSystem("ScaledPix", {
      path: pathToScaledFolder,
  
      beforeWrite: function(fileObj, readStream) {
          return {
              extension: 'png',   // sets extension of file
              type: 'image/png'   // sets content of type field in db
          };
      },
      transformWrite: function(fileObj, readStream, writeStream) {
          // calculate max possible size if scaled by integer
          // that will fit into maximal width available
          function scaleToMaxByInteger(readStream) {
              gm(readStream)
                  .size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
                  if (err) {
                      // handle the error
                  } else {
                      var maxWidth = 100;
                      var originalWidth = size.width;
                      var scaleInt = Math.floor(maxWidth/originalWidth);
                      var scaleSize = scaleInt * originalWidth;
  
                      console.log('originalWidth: ' + originalWidth);
                      console.log('scaleInt: ' + scaleInt);
                      console.log('scaleSize: ' + scaleSize);
  
                      return scaleSize;
                  }
              }));
          };
  
          // Resizing image with no antialiasing
          gm(readStream)
          .sample(scaleToMaxByInteger(readStream)) // &gt;&gt;&gt;&gt;&gt;&gt; This is NOT WORKING
          .stream('PNG')
          .pipe(writeStream);
      }
  });
  </code></pre>