ActiveStorage How to convert image before upload to S3? - ruby-on-rails

I configured active storage to upload has_one_attached :image to Amazon S3.
product.image.attach(io: open(img_url), filename: "file.jpg")
But I want to optimize images: compress them or convert to .webp
I have mini-magick gem which i can use to modify images.
So can I dynamically modify image from url before uploading to Amazon?
Or all can I do is to save image from url, process it and then do attach? (It is so slow when I have millions of images)

According to ActiveStorage, it supports transformation, so you could try using https://github.com/janko/image_processing with mini magick or libvips convert method to transform the image before saving it. Or at least that's what i'm trying to do atm.
If i manage to do it I'll post it here.
Good luck

Related

Processing file before upload using ActiveStorage

How would I process a file before it's uploaded using activestorage. I need to be able to modify an svg file's content before it actually gets uploaded to S3. Can't seem to find any callbacks.
There is no way to do this natively with ActiveStorage. It's the major drawback with using ActiveStorage.
As far as I know, the only way to modify an upload is to create a variant of the original upload after it is created...which creates a (completely different) variant image based on the image that was originally uploaded.
ActiveStorage is easy to setup but, after using it with a few applications, Carrierwave..etc seem like better options.
In addition, if you want to upload in a background job, ActiveStorage is a pain.

Generate a video from many images using RMagick in RoR

I am trying to generate a video from multiple images using RMagick. The images are saved into ActiveStorage and what I am trying to do is
images = Magick::ImageList.new(*["#{url_for(Post.first.image)}","
{url_for(Post.last.image)}"])
images.write("new_video.avi")
but that way seems not really working so basically all I want is calling images from activestorage and display them like a video. Any ideas?
Rmagick doesn't support video formats. You can generate a gif using the ImageList class with some time between each image. Here you can find a lot of good examples.
Alternatively if you really need an AVI file you can switch to other libraries for example ffmpeg. Example here

Find resolution of an image in dpi value uploaded using paperclip

In our Rails Application, we are uploading images using Paperclip gem. I would like to know if there is a way to find DPI value of such an uploaded image?

Carrierwave rename uploaded images

I am using CarrierWave for uploading images. I also have some image cropping that happens later. This process includes sending of the cropped-area dimensions from the client(javascript) and then recreate a cropped image on the server. After this I would like to rename all the version filenames.
Is it possible after the cropping process, I can rename all the various versions. I do not wish to recreate the versions but rather just rename the already existing versions.
You can rewrite method in your uploader
def filename
"new_name_for_file" if file
end

Uploading Images without using ImageMagick

I don't want to have to configure imagemagick but want to use a gem to be able to upload a picture from a form and put it into a database. Are there other options out there? I tried dragonfly and carrierwave but they require ImageMagick.
I often use Blitline which allows we to me to make any edits to the image (if needed) and then I have Blitline store it in an Amazon S3 bucket and place the image url in my database to save space in the database.

Resources