{"action":"created","issue":{"url":"https://api.github.com/repos/lovell/sharp/issues/233","labels_url":"https://api.github.com/repos/lovell/sharp/issues/233/labels{/name}","comments_url":"https://api.github.com/repos/lovell/sharp/issues/233/comments","events_url":"https://api.github.com/repos/lovell/sharp/issues/233/events","html_url":"https://github.com/lovell/sharp/issues/233","id":86342585,"number":233,"title":"Is there a way to resize an image ONLY if the image is smaller or bigger than specified size?","user":{"login":"aukan","id":235955,"avatar_url":"https://avatars.githubusercontent.com/u/235955?v=3","gravatar_id":"","url":"https://api.github.com/users/aukan","html_url":"https://github.com/aukan","followers_url":"https://api.github.com/users/aukan/followers","following_url":"https://api.github.com/users/aukan/following{/other_user}","gists_url":"https://api.github.com/users/aukan/gists{/gist_id}","starred_url":"https://api.github.com/users/aukan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aukan/subscriptions","organizations_url":"https://api.github.com/users/aukan/orgs","repos_url":"https://api.github.com/users/aukan/repos","events_url":"https://api.github.com/users/aukan/events{/privacy}","received_events_url":"https://api.github.com/users/aukan/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/lovell/sharp/labels/question","name":"question","color":"fef2c0"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-08T21:43:19Z","updated_at":"2015-06-10T20:53:36Z","closed_at":"2015-06-10T20:53:36Z","body":"I tried the max() and min() but of course those methods always resize the image and what I'm trying to find is how to tell sharp to only resize the image if (i.e.) the image is bigger than 640w and in any other case, leave the image as is."},"comment":{"url":"https://api.github.com/repos/lovell/sharp/issues/comments/110910359","html_url":"https://github.com/lovell/sharp/issues/233#issuecomment-110910359","issue_url":"https://api.github.com/repos/lovell/sharp/issues/233","id":110910359,"user":{"login":"aukan","id":235955,"avatar_url":"https://avatars.githubusercontent.com/u/235955?v=3","gravatar_id":"","url":"https://api.github.com/users/aukan","html_url":"https://github.com/aukan","followers_url":"https://api.github.com/users/aukan/followers","following_url":"https://api.github.com/users/aukan/following{/other_user}","gists_url":"https://api.github.com/users/aukan/gists{/gist_id}","starred_url":"https://api.github.com/users/aukan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aukan/subscriptions","organizations_url":"https://api.github.com/users/aukan/orgs","repos_url":"https://api.github.com/users/aukan/repos","events_url":"https://api.github.com/users/aukan/events{/privacy}","received_events_url":"https://api.github.com/users/aukan/received_events","type":"User","site_admin":false},"created_at":"2015-06-10T20:53:35Z","updated_at":"2015-06-10T20:53:35Z","body":"Thank you Zewy, that actually can solve the problem in case I have the image in my file system. But I was wondering if you could actually do that with streams, for example (using your code):\r\n\r\n```js\r\n// This code is just an idea. It does not currently work with sharp.\r\nvar readStream = fs.createReadStream('/image/path/img.jpg');\r\nvar transform = sharp(readStream).metadata(function(err, metadata ) {\r\n  if ( metadata.width > 640 ) {\r\n    transform.resize(640, null).toFile('output.jpg');\r\n  } else {\r\n    transform.toFile('output.jpg');\r\n  }\r\n  readStream.pipe(transform);\r\n});\r\n```\r\n\r\nI tried this and the metadata callback never returns, which I suppose it means that sharp does not support streams for that. Maybe for a filesystem it's not so obvious why I would want to do this, but what if I have to download the image from a url?:\r\n\r\n```js\r\n// This code is just an idea. It does not currently work with sharp.\r\nvar http = require('http');\r\nhttp.get('http://some.file/in/internet.jpg', function (err, res) {\r\n  var transform = sharp(res).metadata(function( err, metadata ) {\r\n    if ( metadata.width > 640 ) {\r\n      transform.resize(640, null).toFile('output.jpg');\r\n    } else {\r\n      transform.toFile('output.jpg');\r\n    }\r\n    res.pipe(transform);\r\n  });\r\n});\r\n```\r\n\r\nIn this case, in the same request that I did to obtain the image, I'm already processing it. \r\n\r\nPlease correct me if I'm wrong but I believe that right now the only way is to download the image first and then get the metadata and then make the transformation, which is still fast, but it would be faster the way I just posted.\r\n\r\nThe thing is I'm not sure if this is even possible because I know that, in order to get the metadata, you might need to get at least the first chunk of data and maybe by then is impossible to pipe the stream to other streams. Any ideas?"}}