Uzyskanie szerokości i wysokości obrazu w modelu w klejnocie Rubin Paperclip

Próbuje uzyskać szerokość i wysokość przesłanego obrazu, będąc jeszcze w modelu przy początkowym zapisie.

Można to jakoś zrobić? Oto fragment kodu, który testowałem z mojego modelu. Oczywiście, że nie na " instancji.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' 

Więc zasadniczo staram się to zrobić, aby uzyskać niestandardową szerokość i wysokość miniatur na podstawie początkowych wymiarów obrazu.

Jakieś pomysły?
Author: JakeGould, 2010-05-04

2 answers

/ Align = "left" / Musiałem zrobić proc.

Oto kod z mojego modelu:

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
 25
Author: Corey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-04 21:51:11
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
Author: comonitos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-20 08:04:52