Wednesday, 12 April 2017

How Can I Return Specified Image Size From Google Cloud Storage With Node API?

I'm using cloud storage and it is working well, I just don't want to return the full-size image every time. I'm wondering if there is a way to specify a width to resize an image to when I request it?

Here is the WORKING code. I just need to return smaller images, say, 300px wide (maintaining original aspect ratio). This number could change or fluctuate, so I don't want to save a ton of different versions at different sizes unless I absolutely have to.

var fileName = "Image.png";

var stream = bucket.file(fileName).createReadStream();

res.writeHead(200, { 'Content-Type': imageInfo.mime_type });

stream.on('data', function (data) {
  res.write(data);
});

stream.on('error', function (err) {
  console.log('error reading stream', err);
});

stream.on('end', function () {
  res.end();
});

I've found this site explaining how to do this in PHP, so I'm guessing there is probably a way to do this in node?

https://cloud.google.com/appengine/docs/standard/php/googlestorage/images



via Luke Schlangen

No comments:

Post a Comment