Javascript 如何将缓冲区传递到node.js中的imagemin模块?
在旧版本的imagemin中,我能够向模块传递一个缓冲区,如下所示:Javascript 如何将缓冲区传递到node.js中的imagemin模块?,javascript,node.js,deprecated,imagemin,Javascript,Node.js,Deprecated,Imagemin,在旧版本的imagemin中,我能够向模块传递一个缓冲区,如下所示: new ImageMinify() .src(StreamOrBuffer) .use(ImageMinify.jpegtran({progressive: true})) 在imagemin的当前版本中,没有src函数,调用该模块将导致承诺 我找不到如何在较新版本的imagemin中实现相同的结果 这是可以做到的还是取消了对它的支持?我从github repo那里得到了答案。我在这里发布答案,以防其他人遇到
new ImageMinify()
.src(StreamOrBuffer)
.use(ImageMinify.jpegtran({progressive: true}))
在imagemin的当前版本中,没有src函数,调用该模块将导致承诺
我找不到如何在较新版本的imagemin中实现相同的结果
这是可以做到的还是取消了对它的支持?我从github repo那里得到了答案。我在这里发布答案,以防其他人遇到同样的问题:
可以使用提供缓冲区。从未支持过Streams。这里有一个函数:
-使用sharp在给定大小内调整图像大小
-使用带有缓冲区的imagemin压缩图像(将流转换为缓冲区以压缩图像)
-保存缓冲区 我没有找到使imagemin与style流兼容的好方法,因此它使用临时可写流来存储缓冲区
/**
* It:
* 1. Resize the image directly from a GCP read stream to a 500x500 keeping the aspect ratio
* 2. Create a temp Writable stream to transform it to buffer
* 3. Compress the image using 'imagemin'
* 4. Save it to GCP back
* 5. Delete original image
*
* @param filePath
* @param width
* @param height
*/
const resize = async (
bucket: Bucket,
filePath: string,
width: number,
height: number
): Promise<[File, string]> => {
const ext = path.extname(filePath)
const fileName = path.basename(filePath, ext)
const [imageMetadata] = await bucket.file(filePath).getMetadata()
const metadata = {
contentType: imageMetadata.contentType,
predefinedAcl: 'publicRead',
}
const newFileName = `${fileName}_${width}x${height}${ext}`
const thumbFilePath = path.join(path.dirname(filePath), newFileName)
const outputFile = bucket.file(thumbFilePath)
const bufferData: any[] = []
const tempWritableStream = new stream.Writable()
tempWritableStream._write = function(chunk, encoding, done) {
bufferData.push(chunk)
done()
}
const pipeline = sharp()
bucket
.file(filePath)
.createReadStream()
.pipe(pipeline)
pipeline
.resize(width, height, { fit: 'inside' })
.jpeg({ progressive: true, force: false })
.png({ progressive: true, force: false })
.pipe(tempWritableStream)
return await new Promise((resolve, reject) =>
tempWritableStream
.on('finish', async () => {
const transformedBuffer = await minimizeImageFromBufferArray(
bufferData
)
await saveImage(transformedBuffer, outputFile, metadata)
await bucket.file(filePath).delete()
resolve([outputFile, newFileName])
})
.on('error', reject)
)
}
/**
*它:
* 1. 直接将图像从GCP读取流调整为500x500,并保持纵横比
* 2. 创建临时可写流以将其转换为缓冲区
* 3. 使用“imagemin”压缩图像
* 4. 将其保存回GCP
* 5. 删除原始图像
*
*@param文件路径
*@参数宽度
*@param高度
*/
const resize=async(
桶:桶,
filePath:string,
宽度:数字,
身高:多少
):承诺=>{
const ext=path.extname(文件路径)
const fileName=path.basename(filePath,ext)
const[imageMetadata]=wait bucket.file(filePath).getMetadata()
常量元数据={
contentType:imageMetadata.contentType,
预定义的ACL:“publicRead”,
}
const newFileName=`${fileName}{u${width}x${height}${ext}`
const thumbFilePath=path.join(path.dirname(filePath),newFileName)
const outputFile=bucket.file(thumbFilePath)
常量缓冲数据:任意[]=[]
const tempWritableStream=new stream.Writable()
tempWritableStream.\u write=函数(块、编码、完成){
bufferData.push(块)
完成()
}
常量管道=夏普()
水桶
.file(文件路径)
.createReadStream()文件
.管道
管道
.resize(宽度、高度,{fit:'inside'})
.jpeg({progressive:true,force:false})
.png({progressive:true,force:false})
.pipe(tempWritableStream)
返回等待新承诺((解决、拒绝)=>
临时可写流
.on('finish',async()=>{
const transformedBuffer=wait minimizemagefromBufferArray(
缓冲数据
)
等待保存映像(transformedBuffer、outputFile、元数据)
等待bucket.file(filePath).delete()
解析([outputFile,newFileName])
})
.on('错误',拒绝)
)
}