开发者

Paperclip obtain real image size

My problem is the next:

I am trying resize a image size depending a proportial size. Example If i have a image whose size is 1440*1000 its new size will be 648*440 (I use a proportion depending a max_size)

NOTE: Then i post my code so you will understand开发者_StackOverflow社区 the size relations.

Ok. so I am reading this stackoverflow post:

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

Now i post my code and then i will describe my problem.

 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

My problem is how could i obtain the "real_size"?. i.e, if image size is "1440*1000" to obtain this size (no attachment size)

UPDATE:

I am thinking a solution. So i think in declare two temp variable to ProductImage model and during initialize method use a before_post_process paperclip callback.

    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

Then i could use this size in the other method.

My new problem is how could I determine the remote_original_photo_path in model?

in controller i use the params[:product][:product_images_attributes][index][:photo].

I could save the temp path in model. However because my real_size method during initilize i don´t know how to pass the params info.

Thanks in advance again


With using a gem like image_size ?

[EDIT]

To determine the original upload path may be you can use :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜