Firebase 存储看起来非常酷且易于使用,但我想知道是否有办法在将图像上传到 Firebase 存储之前调整其大小,例如,在服务器中使用 ImageMagick 运行一个过程,然后使用 Firebase SDK 运行上传过程但是我注意到 Firebase SDK Server 没有存储功能。
3 回答
您还可以使用 Firebase Storage + Google Cloud Functions上传图片、调整图片大小(使用 ImageMagick 或类似工具),然后将其写回 Firebase Storage。
我在这里有一个一起使用这些的示例(尽管我触发了Cloud Vision API,而不是图像调整器)。
附注:Firebase 存储没有 Node.js 客户端,我们建议使用GCloud-Node库。
使用 JavaScript 客户端处理调整大小
我们正在使用JavaScript resizing,它使用客户端设备处理能力来调整图像大小,效果很好,为我们节省了空间和金钱,节省了网络不必要的带宽,并且在大多数移动情况下为客户端节省了大量时间和金钱。
我们使用的原始脚本来自这里,使用它的过程很简单:
<input type="file" id="select">
<img id="preview">
<script>
document.getElementById('select').onchange = function(evt) {
ImageTools.resize(this.files[0], {
width: 320, // maximum width
height: 240 // maximum height
}, function(blob, didItResize) {
// didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
document.getElementById('preview').src = window.URL.createObjectURL(blob);
// you can also now upload this blob using an XHR.
});
};
</script>
我相信那个人(dcollien)值得很多优点,所以我建议你投票支持他的答案。
您还可以使用 Firebase Storage + 官方 FirebaseResize Image
扩展:
https ://firebase.google.com/products/extensions/firebase-storage-resize-images 。这里的缺点是“要安装扩展,您的项目必须在 Blaze(即用即付)计划中”。Bc 基本上它会为您启动一个 Firebase 功能应用程序,每次您将文件上传到您指定的路径时都会触发它。您可以配置所需的大小和格式,或者是否要保留原始文件的副本。好东西你不需要担心底层的实现。