ruby on rails - Paperclip obtain real image size -


my problem next:

i trying resize image size depending proportial size. example if have image size 1440*1000 new size 648*440 (i use proportion depending max_size)

note: post code understand size relations.

ok. reading stackoverflow post:

getting width , height of image in model in ruby paperclip gem

now post code , describe 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 increment or decrement value 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 how obtain "real_size"?. i.e, if image size "1440*1000" obtain size (no attachment size)

update:

i thinking solution. think in declare 2 temp variable productimage model , during initialize method use 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 use size in other method.

my new problem how determine remote_original_photo_path in model?

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

i save temp path in model. because real_size method during initilize donĀ“t know how pass params info.

thanks in advance again

with using gem image_size ?

[edit]

to determine original upload path may can use :

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

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -