【问题标题】:Resize an image before uploading it to firebase在将图像上传到 Firebase 之前调整其大小
【发布时间】:2016-09-30 03:40:57
【问题描述】:

Firebase 存储看起来非常酷且易于使用,但我想知道是否有办法在将图像上传到 Firebase 存储之前调整其大小,例如,在服务器中使用 ImageMagick 运行进程,然后使用Firebase SDK,但我注意到 Firebase SDK Server 没有存储功能。

【问题讨论】:

标签: firebase imagemagick firebase-storage


【解决方案1】:

使用 JavaScript 客户端处理调整大小

我们正在使用 JavaScript 调整大小,它使用客户端设备处理能力来调整图像大小,效果很好,为我们节省空间和金钱,节省网络不必要的带宽,并为客户端节省大量在大多数移动案例中,时间和金钱也是如此。

我们使用的原始脚本came from here,使用过程很简单:

<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) 值得很多优点,所以我建议你投票支持他的答案。

【讨论】:

  • 你如何将此 blob 上传到 firebase?我收到一个错误:Firebase 存储:索引 0 处的 put 中的参数无效:预期的 Blob 或文件。你如何从 window.URL.createObjectURL(blob) 到可以是 .put() 到 firebase 的东西?
  • 根据 firebase 文档,您应该能够像文件一样上传 blob:firebase.google.com/docs/storage/web/… 我记得,我的实现的最终功能都接受。
【解决方案2】:

您还可以使用 Firebase Storage + Google Cloud Functions 上传图片、调整图片大小(使用 ImageMagick 或类似工具),然后将其写回 Firebase Storage。

我有一个将这些一起使用的示例here(尽管我触发了Cloud Vision API,而不是图像调整器)。

附注:Firebase 存储没有 Node.js 客户端,我们建议使用 GCloud-Node 库。

【讨论】:

猜你喜欢
  • 2018-07-11
  • 2021-11-14
  • 2018-03-14
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 2011-04-16
  • 2015-12-05
  • 1970-01-01
相关资源
最近更新 更多