# 图片压缩器

**URL:** https://meta.discourse.org/t/image-compressor/161163
**Category:** Support
**Created:** [2020年八月17日 16:23 UTC](https://meta.discourse.org/t/image-compressor/161163 "2020-08-17T16:23:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yariv\_Cohen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yariv_cohen/32/186924_2.png) [@Yariv\_Cohen](https://meta.discourse.org/u/Yariv_Cohen)
#### Post date: [2020年八月17日 16:23 UTC](https://meta.discourse.org/t/image-compressor/161163/1 "2020-08-17T16:23:49Z")

</div>

大家好  
在 Discourse 上存储大图片有什么解决方案吗？我的意思是，是否有选项可以在用户上传时自动压缩图片，或者另一个次优方案：每隔几个月将 SSD 上的原图替换为缩小版图片？  
从长远来看，我认为让所有大图片一直占用我的 SSD 空间并不明智。  
目前（我刚建立论坛），我每个月需要额外增加 1GB 的存储空间。  
谢谢

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [2020年八月17日 18:22 UTC](https://meta.discourse.org/t/image-compressor/161163/2 "2020-08-17T18:22:15Z")

</div>

此处最优的解决方案是_不要_将上传的文件存储在你的服务器上。

你是否考虑过使用 S3 或类似的服务？你可以用 CDN 为 S3 提供前端加速，这将最大限度地降低传输成本。这比使用本地 SSD 便宜得多。

---

<div class="post-metadata">

### Author: ![Yariv\_Cohen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yariv_cohen/32/186924_2.png) [@Yariv\_Cohen](https://meta.discourse.org/u/Yariv_Cohen)
#### Post date: [2020年八月17日 18:50 UTC](https://meta.discourse.org/t/image-compressor/161163/3 "2020-08-17T18:50:19Z")

</div>

明白了……谢谢 @Stephen 的回答。我目前使用的是 Amazon Lightsail，价格相当便宜，5 美元的套餐就提供了 40GB 的 SSD 空间，应该足够用第一年。不过我还是想为将来找个更好的解决方案，因为以现在的技术（就像我在自己的网站上手动做的那样），我可以把 3MB 的图片压缩到 60KB 左右（同时也会调整分辨率）。如果能在 Discourse 上也这样做，那我的 40GB 空间岂不是可以一直用下去！不过我想，要实现自动甚至手动处理，应该没那么简单吧 😆

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2020年八月20日 21:52 UTC](https://meta.discourse.org/t/image-compressor/161163/4 "2020-08-20T21:52:42Z")

</div>

是的，有几个站点设置可以控制最大图片尺寸和转换质量——请搜索一下。

我们还有一个 rake 任务，可以遍历并调整在这些站点设置更改之前上传的“过大”图片的大小，但我不确定这是否公开，@cvx？

---

<div class="post-metadata">

### Author: ![Moksh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moksh/32/188137_2.png) [@Moksh](https://meta.discourse.org/u/Moksh)
#### Post date: [2020年八月21日 16:55 UTC](https://meta.discourse.org/t/image-compressor/161163/5 "2020-08-21T16:55:55Z")

</div>

你好，

除了配置云存储（S3）和 CDN 来提供图片服务外，有人有在图片存储到 S3 之前或之后实时转换图片（压缩为渐进式 JPEG）的经验吗？

如果您的 Discourse 实例包含大量图片，[http://webpagetest.org/](http://webpagetest.org/) 会反复提示未使用渐进式 JPEG。如果用户直接在帖子中上传图片，那么在发布之前或之后手动转换或压缩图片会很困难。

我发现了一些利用无服务器 Lambda 函数在 AWS S3 存储桶上优化图片的方法。

**付费方案：**

> **[Optimize and resize images in AWS S3 in real-time with ImageKit](https://imagekit.io/blog/image-optimization-resize-aws-s3-imagekit/)**
>
> Attach your AWS S3 bucket to ImageKit and start delivering perfectly optimized and transformed images in real-time with just a few minutes of effort.

> **[Amazon S3](https://kraken.io/docs/storage-s3)**
>
> Learn how to store optimized images in Amazon S3 using the Kraken.io API. See required parameters and integration examples for S3 buckets.

**自建方案：**  
[https://developer.happyr.com/aws-lambda-image-optimization-with-serverless](https://developer.happyr.com/aws-lambda-image-optimization-with-serverless)

> **[GitHub - sourcey/s3-image-optimizer: Optimize images in large S3 storage buckets](https://github.com/sourcey/s3-image-optimizer)**
>
> Optimize images in large S3 storage buckets

非常想听听大家的经验？

谢谢。
