2013-05-20 16 views
5

Muszę warunkowo utworzyć różne wersje przesłanego obrazu. Wiem, że Carrierwave obsługuje tę funkcję. Ale moje wymagania są nieco trudne.Zmiana rozmiaru obrazu warunkowego za pomocą Carrierwave

Dla każdego przesłanego obrazu potrzebuję utworzyć 2 wersje i trzeba skalować oryginalny obraz na podstawie warunków.

Poniżej kod daje lepszy pomysł, co próbuję zrobić:

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 

Widocznie to nie działa. Carrierwave generuje ten błąd:

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

Próbowałem inną odmianę:

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 

nie rzucać żadnych błędów. Ale to zawsze generuje obraz w retina size (1st warunku)

Więc próbowałem jeszcze jedną odmianę:

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 

to robi rzucać żadnych błędów a także nie utworzono żadnych wersję.

Czy ktoś może mi pomóc?

Aktualizacja:

podstawie sugestii @DMKE, zrobiłem to zmiany, teraz działa dobrze

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 

Odpowiedz

7

Chociaż nie błąd składni, kod ten ma semantyczną wadę:

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

Tutaj :image? && !:is_retina_resolutionzawsze ocenia jako fałszywe (spróbuj !:foo w terminalu IRb), a zatem wersja :updown jest nigdy nie utworzona. To samo wyjaśnienie odnosi się do process foo: [sx,sy], if: !:bar?

Od CarrierWave nie obsługuje opcję :unless (o ile mogę powiedzieć), że jedynym sposobem, aby osiągnąć swój cel są pewne definicje metoda twojej CarrierWave::Uploader::Base podklasy:

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

Dzięki @DMKE, na podstawie Twojej sugestii, wprowadziłem pewne zmiany i teraz działa dobrze. – RameshVel

+3

Jeden jest zadowolony z usług. – DMKE

Powiązane problemy