Delete the original attachment, but save the thumbnail using Paperclip

Is there a configuration option in the folder to scale the original image to a specific size instead of creating a different version of the file?

If a user uploads a 750X750 image, I want to scale it to 500x500. I will never use the 750x750 version, so there is no reason to keep it around.

class User < ActiveRecord::Base has_attached_file :avatar, :styles => { :thumb => "500x500>" } end 
+4
source share
2 answers

There is an easy way to override this. All you have to do is set the style in the original:

 class User < ActiveRecord::Base has_attached_file :avatar, :styles => { :original => "500x500>" } end 

It will not save the original and will not accept the entire input image and will not change it in accordance with your specifications. Then, when you want to access it, you do not need to specify a style.

 image_tag @user.avatar 

Instead:

 image_tag @user.avatar(:thumbnail) 
+6
source

This may not be the most pleasant solution, but it may work. I am curious to know if there is a better solution.

 class User < ActiveRecord::Base has_attached_file :avatar, :styles => { :original => "500x500>" } after_create :delete_original_image def delete_original_image File.delete(self.avatar.path) end end 
0
source

Source: https://habr.com/ru/post/1400274/


All Articles