【问题标题】:Rails paperclip generate thumbnail from other styleRails 回形针从其他样式生成缩略图
【发布时间】:2013-10-24 04:33:10
【问题描述】:

我正在学习 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]

【问题讨论】:

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


    【解决方案1】:

    我偶然发现了一个别人在网上制作并为自己使用的处理器。有关高级使用信息,请访问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
        }
    

    【讨论】:

      【解决方案2】:

      也许这会对你或像我这样的谷歌导致这个问题的人有所帮助。

      https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

      生成/重新生成缩略图

      注意:只重新生成几个定义的样式之一,如 下面的一些示例将导致所有人的路径/网址损坏 其他样式,如果你有 :hash 在你的 paperclip_defaults[:path] 和 :updated_at 在你的 paperclip_defaults[:hash_data] (你有 默认)。除非你真的知道自己在做什么,否则不要这样做。

      您可以(重新)使用 Paperclip 的 rake 批量生成缩略图 任务。使用我们上面的示例类:

      bundle exec rake paperclip:refresh:thumbnails CLASS=User
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多