Devo creare diverse versioni dell'immagine caricata in modo condizionale. So che Carrierwave supporta questa funzione. Ma le mie esigenze sono un po 'complicate.Ridimensionamento condizionale dell'immagine con Carrierwave
Per ogni immagine caricata ho bisogno di creare 2 versioni e ho bisogno di ridimensionare l'immagine originale in base alle condizioni.
Di seguito il codice vi darà una migliore idea di quello che sto cercando di fare:
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
A quanto pare questo non sta funzionando. Carrierwave getta questo errore:
Errno::ENOENT - No such file or directory - #<ActionDispatch::Http::UploadedFile:0xe41d508>
E ho provato un'altra variante:
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
Questo non genera alcun errore. Ma genera sempre l'immagine in retina size
(prima condizione)
Così ho provato ancora una variante:
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
Ciò non gettare alcun errore e, inoltre, non ha creato alcuna versione.
Qualcuno può darmi una mano?
Aggiornamento:
Sulla base del suggerimento di @DMKE, ho fatto questo cambiamento, ora funziona benissimo
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
Grazie @ DMK, in base al tuo suggerimento, ho apportato alcune modifiche e ora funziona correttamente. – RameshVel
Uno è contento di essere al servizio. – DMKE