首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Ruby回形针GEM中获取模型中图像的宽度和高度

在Ruby回形针GEM中获取模型中图像的宽度和高度
EN

Stack Overflow用户
提问于 2010-05-05 03:52:55
回答 2查看 14.3K关注 0票数 17

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

有办法做到这一点吗?

这是我从我的模型中测试的代码片段。当然,它在"instance.photo_width“上失败了。

代码语言:javascript
复制
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' 

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

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-05 05:51:12

啊,想明白了。我只需要做个测试。

下面是我的模型中的代码:

代码语言:javascript
复制
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
票数 26
EN

Stack Overflow用户

发布于 2015-05-20 16:04:52

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2768527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档