【问题标题】:Paperclip obtain real image size回形针获取真实图像大小
【发布时间】:2011-07-20 15:31:56
【问题描述】:

我的问题是下一个:

我正在尝试根据比例大小调整图像大小。示例如果我有一个尺寸为 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 信息。

再次提前感谢

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    使用像 image_size 这样的 gem 吗?

    [编辑]

    要确定原始上传路径可能是你可以使用:

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

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2019-04-25
      • 2011-10-25
      相关资源
      最近更新 更多