[ActiveStorage] Resize original image

1,372 views
Skip to first unread message

Marcio de Jesus

unread,
Nov 21, 2018, 7:53:15 AM11/21/18
to Ruby on Rails: Core
Hello,

Currently with the ActiveStorage:Variant has the image.variant(resize: '100x100').processed method that creates a version of the image without modifying the original image.

It would be very useful if there was a method to modify the original image.

It's possible and viable?

Thanks all

DHH

unread,
Nov 22, 2018, 11:53:47 AM11/22/18
to Ruby on Rails: Core
These uploads are considered immutable. If you want to store something else than the original, you should do the transformation on the original and upload the new image.

Ryan Richardson

unread,
Apr 28, 2019, 4:59:02 AM4/28/19
to Ruby on Rails: Core
I have a small app where I'm allowing uploads of photos from cell phones via ActiveStorage. In order to not require users to change their default cell phone camera settings, I'm currently allowing image uploads of unnecessary file size. Would it be feasible to hook in after the asset data is posted, but before the uploaded asset is initially pushed to cloud storage, in order to reduce the asset's file size to a more reasonable max? 

Ashish Prajapati

unread,
Apr 28, 2019, 5:46:47 AM4/28/19
to Ruby on Rails: Core
Hello Ryan,

As far as, I understand after reading your issue that you want to reduce the size of uploaded image before storing it to the cloud storage.

Ryan, as DHH told in the above thread that uploads are considered immutable so Active Storage doesn't support transforming/resizing the image itself. We'll need to use the ImageMagick gem to do the resizing.

We keep it simple and use a nice gem which does the work for us.

1. We have to add the image_processing gem to your Gemfile.

gem 'image_processing'

2. After adding gem and bundle install, we can use a variant in view like

<%= image_tag image.variant(resize: "100x100") %>

Ryan Richardson

unread,
Apr 29, 2019, 5:55:45 PM4/29/19
to Ruby on Rails: Core
Hi Ashish,
Thanks for your response! Being as the original feature request is 5 months old, I figured it was worth revisiting as a possibility. My problem is not in serving resized assets (still a really awesome feature!) but rather the inefficient use of cloud storage. When someone is uploading photo evidence (live pinball scores in my case), we don't want to tell them they need to change their camera settings when their photos exceed a specified size (it doesn't appear you can natively do this at all on iOS in fact). Ultimately, we don't want to run up a big S3 bill storing assets at 10+ times their necessary resolution. If ActiveStorage is not a candidate for such a feature, is there an alternative option that can be recommended to solve this problem?

Thanks again!
- Ryan

Jeremy Daer

unread,
Apr 29, 2019, 8:28:44 PM4/29/19
to rubyonra...@googlegroups.com
Hey Ryan—

How about resizing after upload and destroying the original?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages