【问题标题】:Getting width and height of image in model in the Ruby Paperclip GEM在 Ruby Paperclip GEM 中获取模型中图像的宽度和高度
【发布时间】:2010-05-04 19:52:55
【问题描述】:

尝试在初始保存时仍在模型中时获取上传图像的宽度和高度。

有什么办法吗?

这是我从我的模型中测试的代码的 sn-p。当然它在“instance.photo_width”上失败了。

has_attached_file :photo,
                      :styles => {
                      :original  => "634x471>",
                      :thumb => Proc.new { |instance|                       
                                  ratio = instance.photo_width/instance.photo_height
                                  min_width   = 142
                                  min_height  = 119
                                  if ratio > 1
                                    final_height  = min_height
                                    final_width   = final_height * ratio
                                  else
                                    final_width   = min_width
                                    final_height  = final_width * ratio
                                  end
                                  "#{final_width}x#{final_height}" 
                                }
                    }, 
                    :storage => :s3,
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => ":attachment/:id/:style.:extension",
                    :bucket => 'foo_bucket' 

所以我基本上是在尝试这样做,以根据初始图像尺寸获得自定义缩略图的宽度和高度。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    啊,想通了。我只需要制作一个 proc。

    这是我模型中的代码:

    class Submission < ActiveRecord::Base
    
      #### Start Paperclip ####
    
      has_attached_file :photo, 
                        :styles => {
                          :original  => "634x471>",
                          :thumb => Proc.new { |instance| instance.resize }
                        }, 
                        :storage => :s3,
                        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                        :path => ":attachment/:id/:style.:extension",
                        :bucket => 'foo_bucket' 
    
      #### End Paperclip ####
    
      def resize     
         geo = Paperclip::Geometry.from_file(photo.to_file(:original))
    
         ratio = geo.width/geo.height  
    
         min_width  = 142
         min_height = 119
    
         if ratio > 1
           # Horizontal Image
           final_height = min_height
           final_width  = final_height * ratio
           "#{final_width.round}x#{final_height.round}!"
         else
           # Vertical Image
           final_width  = min_width
           final_height = final_width * ratio
           "#{final_height.round}x#{final_width.round}!"
         end
      end  
    end
    

    【讨论】:

    • 谢谢,这帮助我获得了图片高度,这使我可以动态地对包含图片的 div 设置高度约束,并在页面重新加载时阻止 div 调整大小:)
    • 你们在使用 Amazon S3 存储时是否尝试过让它工作?您的方法适用于本地存储的东西,但是当我在 S3 中存储的东西上尝试它时,我收到一个错误,说“识别”命令无法识别对象。有什么想法吗?谢谢!
    • 只是为了跟进,我发现了这个stackoverflow.com/questions/3160788/… 现在我重新阅读了您的示例,我看到您包含了 .to_file ,它可以与 S3 存储一起使用。所以基本上,就假装我什么都没说。谢谢!
    • 这是否真的将图像从 S3 拉到我的服务器以进行这些计算?它必须。将这些保存到数据库中似乎是一个更好的主意:Image.heightImage.widthImage.ratio
    • 自 Paperclip 3.x 起,photo.to_file 不再受支持。你可以改用geo = Paperclip::Geometry.from_file(Paperclip.io_adapters.for(photo))
    【解决方案2】:
    class Asset
    
    include Mongoid::Paperclip
    before_save :extract_dimensions
    
    field :width, type: Integer
    field :height, type: Integer
    
    has_mongoid_attached_file :data
    
      def extract_dimensions
        return unless is_image?
    
        tempfile = data.queued_for_write[:original]
        unless tempfile.nil?
          geometry = Paperclip::Geometry.from_file(tempfile)
          self.width = geometry.width.to_i
          self.height = geometry.height.to_i
        end
    
        true # wont save if false 
      end
    
      def is_image?
        data_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      相关资源
      最近更新 更多