【发布时间】:2021-09-21 18:26:26
【问题描述】:
如何在我的 NUXT 应用程序中将图像转换为 WEBP 格式,然后再将它们发送到 AWS S3?
我在我的网站上上传了一张照片,我想在上传到亚马逊网络服务之前将图像从文件输入转换为WEBP 格式。与我可以导入sharp并使用它将图像转换为WEBP格式的NodeJS不同,这里不是这种情况,因为我收到如下错误
Failed to compile with 4 errors friendly-errors 01:16:19
These dependencies were not found: friendly-errors 01:16:19
friendly-errors 01:16:19
* child_process in ./node_modules/detect-libc/lib/detect-libc.js, ./node_modules/sharp/lib/libvips.js friendly-errors 01:16:19
* fs in ./node_modules/detect-libc/lib/detect-libc.js, ./node_modules/sharp/lib/libvips.js friendly-errors 01:16:19
friendly-errors 01:16:19
To install them, you can run: npm install --save child_process fs
我想像下面的代码一样转换图像
drop(e) {
e.preventDefault();
e.stopPropagation();
e.target.classList.remove('solid');
const files = e.dataTransfer.files;
this.handleFiles(files);
},
onFilePicked(e) {
e.preventDefault();
e.stopPropagation();
const files = e.target.files;
this.handleFiles(files);
},
saveToBackend(file, result) {
// compress image
// save to aws
},
readFiles(file) {
const reader = new FileReader();
reader.readAsDataURL(file)
reader.onload = () => {
const uploading = this.uploading
const result = reader.result
uploading.push(result)
this.uploading = uploading
// upload to aws
this.saveToBackend(file, result)
}
},
handleFiles(files) {
const Files = Array.from(files);
Files.forEach(file => {
// check if image is a valid image
if (file.type.includes('image')) {
// display the image
return this.readFiles(file)
}
return
});
console.log(Files, "loaded files");
},
对于锋利的插件
import vue from "vue"
import sharp from "sharp"
vue.use(sharp)
请问如何压缩图片?
【问题讨论】:
-
@kissu 您的回答只能解释为什么我正在做的事情行不通,但它没有提供任何解决方案。但我最终找到了一个解决方案,在将图像发送到 AWS S3 之前,无需使用任何包即可将任何图像转换为 WEBP 格式。我必须将图像转换为画布,然后将图像转换为 WEBP 格式。我将在下面发布我的解决方案。
-
到目前为止,我无法解释如何在 StackOverflow 答案中的 2 个应用程序之间设置服务器。很高兴您找到了将图像转换为
.webp的方法,希望在此过程中不会有太多质量损失。渴望看到你的答案。
标签: node.js amazon-web-services vue.js nuxt.js