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

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-13 04:39:32

问题


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


回答1:


This article should get you off on the right foot: “Uploading, Resizing and Serving images with Google Cloud Platform” @Lipis https://medium.com/google-cloud/uploading-resizing-and-serving-images-with-google-cloud-platform-ca9631a2c556



来源:https://stackoverflow.com/questions/43382043/how-can-i-return-specified-image-size-from-google-cloud-storage-with-node-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!