2013-05-20 19 views
5

Ich muss bedingt verschiedene Versionen des hochgeladenen Bildes erstellen. Ich weiß, Carrierwave unterstützt diese Funktion. Aber meine Anforderungen sind ein bisschen schwierig.Bedingte Bildgrößenanpassung mit Carrierwave

Für jedes hochgeladene Bild muss ich 2 Versionen erstellen und das Originalbild basierend auf Bedingungen skalieren.

Im Folgenden Code erhalten Sie eine bessere Vorstellung davon, was ich zu tun versucht:

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 

Anscheinend funktioniert nicht. Carrierwave wirft diesen Fehler:

Errno::ENOENT - No such file or directory - #<ActionDispatch::Http::UploadedFile:0xe41d508>

Und habe ich versucht, eine andere 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 

Dies sind keine Störungen wirft. Aber es erzeugt immer das Bild in retina size (erste Bedingung)

Also versuchte ich eine weitere Variation:

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 

Diese sieht nicht einen Fehler werfen und auch keine Version erstellt.

Kann mir jemand helfen?

Update:

Basierend auf den Vorschlag von @DMKE, ich diese Änderungen vorgenommen haben, jetzt funktioniert es gut

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 

Antwort

7

Obwohl kein Syntaxfehler, dieser Code hat ein semantischer Fehler:

version :updown, :if => :image? && !:is_retina_resolution do 
    # ... 
end 

Hier :image? && !:is_retina_resolutionimmer wertet Falsch aus (versuchen Sie !:foo in einem IRb-Terminal), so ist die :updown-Version nie erstellt. Die gleiche Erklärung gilt für process foo: [sx,sy], if: !:bar?

Da CarrierWave keine :unless Option nicht unterstützt (soweit ich das beurteilen kann), den einzigen Weg, um Ihr Ziel zu erreichen, sind einige Methodendefinitionen in Ihrer CarrierWave::Uploader::Base Unterklasse:

process resize_to_fill: [667*2, 778*2], if: :image_and_retina? 
process resize_to_fill: [667, 778],  if: :image_and_not_retina? 

def image_and_retina?(img) 
    image?(img) && is_retina_resolution(img) 
end 

def image_and_not_retina?(img) 
    image?(img) && !is_retina_resolution(img) 
end 
+1

Dank @DMKE, basierend auf Ihrem Vorschlag, habe ich einige Änderungen vorgenommen, und es funktioniert jetzt gut. – RameshVel

+3

Man ist froh, um zu dienen. – DMKE