2013-05-20 9 views
5

के साथ सशर्त छवि का आकार बदलना मुझे अपलोड की गई छवि के विभिन्न संस्करणों को सशर्त रूप से बनाना होगा। मुझे पता है कैरियरवेव इस सुविधा का समर्थन करता है। लेकिन मेरी आवश्यकताओं थोड़ा मुश्किल है।कैरियरवेव

प्रत्येक अपलोड की गई छवि के लिए मुझे 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 (1 हालत) में छवि उत्पन्न करता है

तो मैं एक और भिन्नता की कोशिश की:

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 

यह does not किसी भी त्रुटि फेंक और यह भी किसी भी संस्करण नहीं बनाया।

क्या कोई मेरी मदद कर सकता है?

अद्यतन:

@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 

उत्तर

7

हालांकि नहीं एक सिंटैक्स त्रुटि, इस कोड को एक semantical दोष है:

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

, :image? && !:is_retina_resolutionहमेशा झूठे का मूल्यांकन करता है (एक आईआरबी टर्मिनल में !:foo आज़माएं), इस प्रकार :updown संस्करण कभी बनाया गया है। एक ही विवरण के लिए process foo: [sx,sy], if: !:bar?

चला जाता है के बाद से CarrierWave एक :unless विकल्प (जहाँ तक मैं बता सकता है), अपने लक्ष्य को प्राप्त करने का एकमात्र तरीका का समर्थन नहीं करता अपने CarrierWave::Uploader::Base उपवर्ग में कुछ विधि परिभाषाएं दी गई हैं:

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

धन्यवाद @ डीएमकेई, आपके सुझाव के आधार पर, मैंने कुछ बदलाव किए हैं, और यह अभी ठीक काम करता है। – RameshVel

+3

सेवा के होने में खुशी है। – DMKE

संबंधित मुद्दे