我正在尝试调整用户上传的图像大小,以使我的网站更高效并限制我的存储使用量。
我从包中创建了一个名为 resizeFile 的函数"react-image-file-resizer"
现在我正在努力解决的是如何在图像上传到 Firebase 存储之前调整图像大小?
这是我的代码:
const resizeFile = (file) =>
new Promise((resolve) => {
Resizer.imageFileResizer(file, 500, 500, "JPEG", 100, 0, (uri) => {
resolve(uri);
});
});
const addProperty = async () => {
if (
!propName ||
) {
alert("Please Enter All Required Data");
} else {
await firebase
.firestore()
.collection("Properties")
.add({
propname: propName,
})
.then(async (result) => {
await Promise.all(
selectedImages.map(async (image) => {
const uri = await resizeFile(image);// i dont know what to do after this point
console.log(uri);
const storageRef = storage.ref(
`propertyImages/${result.id}/${image.name}`
);
storageRef.put(uri).then((urls) => {
console.log(urls);
storageRef.getDownloadURL().then( (downloadUrls) => {
console.log(downloadUrls);
firebase
.firestore()
.collection("Properties")
.doc(result.id)
.update({
propertyID: result.id,
imageUrls: firebase.firestore.FieldValue.arrayUnion(downloadUrls)
})
.then((res) => {
//handleUploadChange();
// alert("Property Added Successfully");
// window.location.reload();
});
});
});
})
);
});
}
};
编辑:
好的,我设法解决了这个问题,但仍然存在一个问题,即由于某种原因,高度永远不会调整到我指定的 maxHeight 。这是为什么?
这是我修改的内容:
await Promise.all(
selectedImages.map(async (image) => {
const uri = await resizeFile(image);
console.log(uri);
const storageRef = storage.ref(
`propertyImages/${result.id}/${image.name}`
);
storageRef.putString(uri,'data_url').then((urls) => {
console.log(urls);
storageRef.getDownloadURL().then( (downloadUrls) => {
console.log(downloadUrls);
firebase
.firestore()
.collection("Properties")
.doc(result.id)
.update({
propertyID: result.id,
imageUrls: firebase.firestore.FieldValue.arrayUnion(downloadUrls)
})
.then((res) => {
//handleUploadChange();
// alert("Property Added Successfully");
// window.location.reload();
});
});
});
})
);