           .toBuffer (err, outputBuffer, info) ->
             if err
               return callback err
-            originalWidth = metadata.width
-            resizedWidth = info.width
-            factor = originalWidth / resizedWidth
+            if width
+              originalWidth = metadata.width
+              resizedWidth = info.width
+              factor = originalWidth / resizedWidth
+            else
+              originalHeight = metadata.height
+              resizedHeight = info.height
+              factor = originalHeight / resizedHeight
             out.buffer.send outputBuffer
             out.factor.send factor
             do callback

  , (payload, groups, out, callback) ->
    width = c.params.width
    height = c.params.height
    if not width? and not height?
      width = 256
    try
      inputBuffer = sharp payload
      inputBuffer.metadata (err, metadata) ->
        if err
          return callback err
        # Try to preserve the same format, if there's EXIF
        if metadata.exif?
          inputBuffer

          .toBuffer (err, outputBuffer, info) ->
            if err
              return callback err
            if width
              originalWidth = metadata.width
              resizedWidth = info.width
              factor = originalWidth / resizedWidth
            else
              originalHeight = metadata.height
              resizedHeight = info.height
              factor = originalHeight / resizedHeight
            out.buffer.send outputBuffer
            out.factor.send factor
            do callback
