关于将图像上载和调整大小到firebase函数有很多描述。但是我想相反。
我想通过firebase函数传递图像作为对请求的答复。 而且我想使用cors!
我真的很感动,所以能得到任何帮助。
答案 0 :(得分:1)
这是使用TypeScript和Puppeteer捕获屏幕截图并设置对屏幕截图的响应的完整Firebase功能代码。
import * as functions from 'firebase-functions';
import * as puppeteer from "puppeteer";
const runtimeOpts = {
timeoutSeconds: 120,
memory: <const> '1GB'
};
export const search = functions.runWith(runtimeOpts).https.onRequest(async (request, response) => {
try {
const imageBuffer: Buffer = await generateScreenShot();
response.writeHead(200, {'Content-Type': 'image/png'});
response.write(imageBuffer.toString('binary'), 'binary');
response.end()
} catch (err) {
console.error(err);
}
})
function generateScreenShot(): Promise<Buffer> {
return new Promise<Buffer>(async (resolve, reject) => {
try {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://example.ee', {
waitUntil: 'networkidle2'
});
await page.waitFor(500);
const imageBuffer: Buffer = await page.screenshot({fullPage: true});
await browser.close();
resolve(imageBuffer);
} catch (err) {
reject(err);
}
});
}