ruby-on-rails - Rails 回形针从其他样式生成缩略图

标签 ruby-on-rails ruby-on-rails-3 paperclip jcrop

我正在遵循 jcrop Rails 教程,但遇到了障碍。归根结底是回形针正在从原始文件生成缩略图,但我需要它从另一种样式生成。原始文件的产品照片和文档边缘之间没有任何空格。因此我无法进一步裁剪。为了解决这个问题,我制作了另一种具有白色像素填充的样式。这就是我想要生成缩略图的内容。

# croppable is the one with the padding...it's what shows up in the crop view.
# I want :thumb to be generated from THAT style, not :original.
# When generating from :original, the crop offset/size is screwed because the dimensions of :original don't match :cropped
# and I can't crop beyond the pixel dimensions of :original.
has_attached_file :photo, :styles => {
                    :thumb => { :geometry => "300x300#", :format => :jpg, :processors => [:cropper] },
                    :general => ["150x375", :jpg],
                    :show => ["x425", :jpg],
                    :croppable => ["1200x1200>", :jpg]
        },
        :url  => "/assets/wines/:style/:wine_name",
        :path => ":rails_root/public:url",
        :default_url => ":wine_default",
        :default_path => ":rails_root/public:wine_default",
        :default_style => :show,
        :convert_options => {
            :thumb => '-gravity center -rotate -30',
            :croppable => '-gravity center -extent 1200x1200',
            :general => '-gravity center -extent 150x375 -quality 95',
            :all => '-quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0'
        },
        :processors => [:thumbnail, :compression]

最佳答案

我在网上发现了一个别人制作的处理器并为自己使用。详情可查看http://pjkh.com/articles/speeding-up-thumbnail-generation-with-paperclip/获取高级使用信息。

recursive_thumbnail.rb(包含在 lib/paperclip_processors 中)

module Paperclip
    class RecursiveThumbnail < Thumbnail
      def initialize file, options = {}, attachment = nil
        super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment
      end
    end
end

对于您的模型:

:styles => {
    :croppable => ["1200x1200>", :jpg],
    :show => ["x425", :jpg],
    :general => ["150x375", :jpg],
    :thumb => {
        :geometry => "150x150^",
        :format => :jpg,
        :processors => [:recursive_thumbnail] },
        :thumbnail => :croppable
    }

关于ruby-on-rails - Rails 回形针从其他样式生成缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19556993/

相关文章:

ruby-on-rails - 我在 Rails 中使用 Figaro gem 时遇到错误

ruby-on-rails-3 - Multi-Tenancy SaaS的框架

ruby-on-rails - counter_cache 可以与 has_many 一起使用吗?

ruby-on-rails-3 - 在 rails3 中一起使用 mongodb 和 redis

ruby-on-rails - 回形针+ ActionMailer-添加附件?

ruby-on-rails - 如果用户没有上传图像,回形针头像图像就会丢失

ruby-on-rails - Rails 如何显示 JSON 请求的哈希值

ruby-on-rails - osx 10.8.5 - Ruby on Rails - rails s 上的 rmagick 错误

ruby-on-rails - Rails - 用户在创建对象后按下 'Back',创建重复项

ruby-on-rails - 使用 PaperClip AV 上传后视频显示黑屏