Resize missing.png depending upon style in papercl

2019-04-10 19:20发布

问题:

I'm using Paperclip to upload a image

here my paperclip configuration

has_attached_file :avatar, 
                    :path => ":rails_root/public/users/:id/avatar/:style/avatar.jpg",
                    :url => "/users/:id/avatar/:style/avatar.jpg",
                    :default_url => "/missing/users/:style/missing.png",
                    :styles => {"47x47" => "47x47", "228x228" => "228x228","185x176"=>"185x176","pitch_planner"=>"262x129!"},
                    :convert_options => {"47x47" => "-background black -gravity center -extent 47x47",
                      "228x228" => "-background black -gravity center -extent 228x228","185x176" => "-background black -gravity center -extent 185x176"}

Now what if I want is to generate a resize image of missing.png depending upon the "style" How to achieve this in paperclip

One way to do it resize the image manually and store it inside folder pitch_planner or what ever styles you want to resize for

can it be done in programmatically through paperclip

回答1:

Not with paperclip, but you could overwrite the method that looks for the default image, and use image magick to create it if not already present.

img = Magick::Image::read(default_image).first
img.resize_to_fit(75, 75)
img.write 'path'