我需要有條件地創建不同版本的上傳圖像。我知道Carrierwave支持這個功能。但我的要求有點棘手。帶載波調整大小的條件圖像
對於每個上傳的圖片,我需要創建2個版本,並需要根據條件縮放原始圖像。
下面的代碼會給你更好的主意,我想做的事:
version :leftright, :if => :image? do
process :resize_to_fill => [667*2, 778*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [667, 778] ,:if => !:is_retina_resolution?
end
version :updown, :if => :image? do
process :resize_to_fill => [1024*2, 487*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 487] ,:if => !:is_retina_resolution?
end
#resize the original image
process :resize_to_fill => [1024*2, 768*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 768] ,:if => !:is_retina_resolution?
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(new_file.file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
顯然,這是行不通的。 Carrierwave拋出這個錯誤:
Errno::ENOENT - No such file or directory - #<ActionDispatch::Http::UploadedFile:0xe41d508>
我試過另一種變化:
version :leftright, :if => :image? do
if :is_retina_resolution?
process :resize_to_fill => [667*2, 778*2]
else
process :resize_to_fill => [667, 778]
end
end
version :updown, :if => :image? do
if :is_retina_resolution?
process :resize_to_fill => [1024*2, 487*2]
else
process :resize_to_fill => [1024, 487]
end
end
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(new_file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
這不會引發任何錯誤。但它總是在retina size
(第一狀態)生成圖像
於是,我就多了一個變化:
version :leftright, :if => :image? && :is_retina_resolution do
process :resize_to_fill => [667*2, 778*2]
end
version :leftright, :if => :image? && !:is_retina_resolution do
process :resize_to_fill => [667, 778]
end
version :updown, :if => :image? && :is_retina_resolution do
process :resize_to_fill => [1024*2, 487*2]
end
version :updown, :if => :image? && !:is_retina_resolution do
process :resize_to_fill => [1024, 487]
end
,這並不拋出任何錯誤,也不會創建任何版本。
有人可以幫我嗎?
更新:
基於由@DMKE的建議,我做了這個變化,現在它工作正常
version :leftright, :if => :image? do
process :resize_to_fill => [667*2, 778*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [667, 778] ,:if => :is_not_retina_resolution?
end
version :updown, :if => :image? do
process :resize_to_fill => [1024*2, 487*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 487] ,:if => :is_not_retina_resolution?
end
#resize the original image
process :resize_to_fill => [1024*2, 768*2] ,:if => :image_and_retina?
process :resize_to_fill => [1024, 768] ,:if => :image_and_not_retina?
process :if => :not_image?
def image_and_retina?(img)
is_img = image? img
return false unless is_img
return is_retina_resolution?(img)
end
def image_and_not_retina?(img)
is_img = image? img
return false unless is_img
return !is_retina_resolution?(img)
end
# returns true if image file
def image?(new_file)
self.file.content_type.include? 'image'
end
def not_image?(new_file)
!self.file.content_type.include? 'image'
end
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
def is_not_retina_resolution?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] < 1536 and image[:width] < 2048
end
謝謝@ DMKE,根據你的建議,我做了一些改變,現在工作正常。 – RameshVel
一個很高興成爲服務。 – DMKE