私の問題は次です:ペーパークリップ得る実際の画像サイズ
私はproportialサイズによっては、画像の大きさのサイズを変更しようとしています。例サイズが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」を取得する方法です。 すなわち、画像サイズがこのサイズ(なし添付ファイルのサイズ)
UPDATE得るために、 "* 1000 1440" の場合:
私は解決策を考えています。だから私はProductImage
モデルとinitialize
メソッドの間にペーパークリップコールバックを使用して2つの一時変数を宣言すると思います。
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
メソッドはinitilize中に私はparams
情報を渡す方法を知らないので。 image_sizeのような宝石を使用して再び
私は私のポストを更新します。もう一度お読みください。 – maxiperez
私の投稿も更新する – Awea