2011-07-20 28 views
1

我的問題是下一個:回形針獲得真實圖像大小

我想調整大小的圖像大小取決於proportial大小。示例如果我有一個尺寸爲1440 * 1000的圖像,其新尺寸將爲648 * 440(我使用的比例取決於max_size)

注意:然後我發佈我的代碼,以便了解尺寸關係。

好的。所以我讀這個計算器後:

Getting width and height of image in model in the Ruby Paperclip GEM

現在我後我的代碼,然後我將說明我的問題。

class ProductImage < ActiveRecord::Base 
     belongs_to :product, :dependent => :destroy 

     MAXIMUM_SIZE = 650 

     has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:real_size => Proc.new { |instance| instance.real_size }, :original => "400x400>", :medium => "300x300>", :principal => "240x240>", :thumb => "100x100>", :small => "80x50>"} 

    def real_size 
     #image = Paperclip::Geometry.from_file(photo.to_file(:maximum_size)) 
     #OBTAIN REAL IMAGE SIZE, NOT ATTACHMENT SIZES 
if image_less_than_maximum_size? 
      return "#{image.width}x#{image.height}" 
     else 
      return adjust_image_size(self.width, self.height) 
     end 
     end 

     def adjust_image_size(image_width, image_height) 
     ratio       = (image_width/image_height).to_f 
     difference_between_size   = (image_width - image_height).abs 
     percentage_difference   = ratio > 1 ? difference_between_size * 100.0/image_width : difference_between_size * 100.0/image_height 
     difference_respect_maximum_size = ratio > 1 ? MAXIMUM_SIZE * 100.0/image_width : MAXIMUM_SIZE * 100.0/image_height 
     width       = height = 0.0 

     if ratio > 1 
      #USE 101.0 FOR INCREMENT OR DECREMENT THE VALUE A LITTLE BIT 
      width = image_width * difference_respect_maximum_size/101.0 
      height = width - (percentage_difference * width/101.0) 
     else 
      heigth = image_height * difference_respect_maximum_size/101.0 
      width = height - (percentage_difference * height/101.0) 
     end 

     return "#{width}x#{height}" 
     end 

     def image_less_than_maximum_size? 
     if self.width > self.height 
      return self.width < MAXIMUM_SIZE 
     else 
      return self.height < MAXIMUM_SIZE 
     end 
     end 
    end 

我的問題是我怎麼能得到「real_size」? 即,如果圖像尺寸爲「1440×1000」獲得該尺寸(無附件大小)

UPDATE:

我想一個解決方案。所以我認爲在申報兩個臨時變量ProductImage模型和在initialize方法使用before_post_process paperclip回調。

class ProductImage < ActiveRecord::Base 
     belongs_to :product, :dependent => :destroy 
     attr_accessor :height, :width 

     MAXIMUM_SIZE = 650 

     has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:real_size => Proc.new { |instance| instance.real_size }, :original => "400x400>", :medium => "300x300>", :principal => "240x240>", :thumb => "100x100>", :small => "80x50>"} 
     before_post_process :image? 
     before_post_process :assign_size 

    ... 

    def assign_size 
      @width = Paperclip::Geometry.from_file(remote_original_photo_path).width 
      @height = Paperclip::Geometry.from_file(remote_original_photo_path).height 
    end 

end 

然後我可以在其他方法中使用這個大小。

我的新問題是如何確定模型中的remote_original_photo_path?在控制器中我使用params[:product][:product_images_attributes][index][:photo]

我可以保存模型中的臨時路徑。但是因爲我的real_size方法在初始化我不知道如何通過params信息。

在此先感謝再次

回答

2

隨着使用像image_size寶石?

[編輯]

要確定原始上傳路徑可能是你可以使用:

remote_original_photo_path = File.basename(upload['datafile'].original_path) 
+0

更新我的職務。請再讀一遍。 – maxiperez

+0

我也更新了我的文章 – Awea