回形针获得真实的图像尺寸

时间:2011-07-20 15:31:56

标签: ruby-on-rails paperclip

我的问题是下一个:

我正在尝试根据比例大小调整图像大小。示例如果我的图像大小为1440 * 1000,则其新大小为648 * 440(我使用的比例取决于max_size)

注意:然后我发布我的代码,以便您了解大小关系。

确定。所以我正在阅读这篇stackoverflow帖子:

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”以获得此尺寸(无附件尺寸)

更新:

我正在考虑解决方案。所以我认为在向ProductImage模型声明两个临时变量时,在initialize方法中使用before_post_process回形针回调。

    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信息。

再次提前致谢

1 个答案:

答案 0 :(得分:2)

使用像image_size这样的宝石?

<强> [编辑]

要确定原始上传路径,您可以使用:

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