# Would it be worth resizing uploaded images (to save space)?

**URL:** https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901
**Category:** Feature
**Created:** [22 oktober 2016 om 18:35 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901 "2016-10-22T18:35:42Z")
**Posts on this page:** 7
**Page:** 2

<div class="post-metadata">

### Author: ![Tom\_Newsom](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tom_newsom/32/115981_2.png) [@Tom\_Newsom](https://meta.discourse.org/u/Tom_Newsom)
#### Post date: [24 april 2019 om 20:07 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/21 "2019-04-24T20:07:26Z")

</div>

> [@mitchellk](#):
>
> Just an update I found a nice way of optimizing images older than X days using GruntJS … going to test the code this weekend and post my results here.

I’d love to know if you had any success here?  
Our install is nearly 3 years old now and the server is showing 27GB used (after running cleanup). There’s quite a lot of photo uploading, often straight from phones, which means lots of multi-MB image files building up.

I’d love to be able to shrink all \>1 year old images to something decent but sane, to claw some precious space back.

---

<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: [24 april 2019 om 22:32 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/22 "2019-04-24T22:32:46Z")

</div>

I believe @neil has some code we used to do this a few times, but it was focused on older versions of Discourse where inappropriately large `.png` images would sometimes get saved instead of smaller `.jpg` images.

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [25 april 2019 om 14:38 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/23 "2019-04-25T14:38:55Z")

</div>

The script is [downsize\_uploads.rb](https://github.com/discourse/discourse/blob/master/script/downsize_uploads.rb). Run it like so:

```plaintext
cd /var/www/discourse
RAILS_ENV=production bundle exec ruby script/downsize_uploads.rb

```

It will try to downsize images to be 1 megapixel or less, or you can pass in the max size you want.

> [@Tom\_Newsom](#):
>
> I’d love to be able to shrink all \>1 year old images

The script doesn’t scope it like that, but you can easily edit the script to do so. Add `.where('created_at < ?, 1.year.ago)` to the [query on line 10](https://github.com/discourse/discourse/blob/master/script/downsize_uploads.rb#L10).

---

<div class="post-metadata">

### Author: ![P16](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@P16](https://meta.discourse.org/u/P16)
#### Post date: [16 mei 2019 om 04:54 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/24 "2019-05-16T04:54:36Z")

</div>

@neil Could this be used for S3 image uploads as well?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [16 mei 2019 om 10:01 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/25 "2019-05-16T10:01:56Z")

</div>

That script only works if you store the images locally. It would need to be updated to support images stored externally.

---

<div class="post-metadata">

### Author: ![Lure](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lure/32/130462_2.png) [@Lure](https://meta.discourse.org/u/Lure)
#### Post date: [10 oktober 2019 om 12:58 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/26 "2019-10-10T12:58:38Z")

</div>

> [@neil](#):
>
> ```plaintext
> RAILS_ENV=production bundle exec ruby script/downsize_uploads.rb
> 
> ```

I tried this on my discourse, but got the following error

`/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql\_connection’: FATAL: Peer authentication failed for user “discourse” (ActiveRecord::NoDatabaseError)`

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [11 oktober 2019 om 08:17 UTC](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901/27 "2019-10-11T08:17:15Z")

</div>

FTR, the script has now been updated to also handle uploads stored on S3.

[Vorige pagina](https://meta.discourse.org/t/would-it-be-worth-resizing-uploaded-images-to-save-space/51901.md?page=1)
