Estoy tratando de cambiar el tamaño de una imagen después de transmitir desde el almacenamiento en la nube de Google. La transmisión funciona bien, pero el cambio de tamaño no parece funcionar. Necesito pasar el objeto de la imagen a la función de cambio de tamaño de sharpjs, que luego cambiará el tamaño y devolverá la imagen en la respuesta de la API.
Mi código es el siguiente:
const express = require('express') const {Storage} = require('@google-cloud/storage'); let server = express() const storage = new Storage(); const bucketName = '<some-bucket-name>' server.get('/*', async (req, res, next) => { // Extract the query-parameter const widthString = req.query.width const heightString = req.query.height const format = req.query.format const fileName = req.path.substring(1); console.log("url: ", req.path) // Parse to integer if possible let width, height if (widthString) { width = parseInt(widthString) } if (heightString) { height = parseInt(heightString) } // Set the content-type of the response res.type(`image/${format || 'png'}`) // Get the resized image downloadFileStream(fileName).then((imageObj) =>{ resize(imageObj, format, width, height).pipe(res) }) .catch ((e) => console.log(e) ); }) async function downloadFileStream(fileName){ return await storage.bucket(bucketName).file(fileName).createReadStream().pipe() }Me falta algo en la forma en que debería llamar al cambio de tamaño después de la descarga del archivo. me sale el siguiente error:
TypeError: Cannot read property 'on' of undefined at PassThrough.Readable.pipe (internal/streams/readable.js:657:8) at streamFile (/home/adityam/imageoptimizer/server.js:40:82) at /home/adityam/imageoptimizer/server.js:30:7Basado en este y este tutorial, sugeriría reescribir su función de cambio de tamaño de esta manera:
incluir biblioteca aguda
const sharp = require("sharp"); obtenga la imagen usando la función downloadFileStream , luego cambie el tamaño
const image = downloadFileStream(fileName) const resized = await sharp(image) .resize({ width: width, height: height }) .toBuffer() .then( data => { //... })Ver también: