2013-10-18 4 views
2

나는 혼자서 그것을 시도한 후에 둘러 보았고 해결책을 찾지 못했습니다. 사용자가 사진을 업로드 할 때 최소 및 최대 크기를 초과하면 사진의 크기가 조정되기를 원합니다. 그러나 두 조건을 원합니다. 가로로 찍은 사진 (동/서)은 내가 설정 한 크기 내에 머물러야하며 높이 찍은 사진 (북/남)에도 동일하게 적용됩니다.높이/너비를 초과하는 경우 반송파로 이미지 크기 조정

예를 들어 사용자가 멀리 서서 크기가 3264x1840 인 사진을 업로드하는 경우입니다. 업로드 크기는 584x329에 맞게 조정해야합니다. 업로드 파일 크기가 584x329보다 작 으면 크기가 조정되지 않습니다.

또 다른 예는 사용자가 키가 큰 사진을 업로드하고 크기가 2448 x 3264 인 경우입니다. 업로드 크기는 247x329에 맞게 조정해야합니다.

필자는 MiniMagick을이 기능과 함께 사용하려고했는데, 이것이 필요하다고 생각했습니다. CarrierWave 만 사용할 수 있다면 완벽합니다.하지만 MiniMagick이 사진 크기를 조정하는 데 사용되기로되어 있다고 생각했습니다.

내가받는 오류는 컨트롤러의 def create에서 '정의되지 않은 메서드 resize' for #<ImageUploader:0x007f8606feb9b8>' and it points to @photo = Photo.new (params [: photo])`입니다.

BTW 크기는 사진을 업로드 할 때 일반적으로 휴대 전화의 기본 크기이기 때문에 높습니다.

image_uploader.rb :

class ImageUploader < CarrierWave::Uploader::Base 

    include CarrierWave::MiniMagick 

storage :file 
    # storage :fog 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 


    process :resize => [584, 329] 

    def resize_to_limit(width, height) 
     manipulate! do |img| 
      img.resize "#{width}x#{height}>" 
      img = yield(img) if block_given? 
      img 
     end 
     end 

    # Create different versions of your uploaded files: 
    version :thumb do 
    process :resize_to_limit => [200, 200] 
    end 

end 

사진 컨트롤러 : 이미지 업 로더에

def create 
    @photo = Photo.new(params[:photo]) 
    @photo.user = current_user 
    if @photo.save 
     flash[:notice] = "Successfully created photos." 
     redirect_to :back 
    else 
     render :action => 'new' 
    end 
    end 

def resize(width, height, gravity = 'Center') 
    manipulate! do |img| 
    img.combine_options do |cmd| 
     cmd.resize "#{width}" 
     if img[:width] < img[:height] 
     cmd.gravity gravity 
     cmd.background "rgba(255,255,255,0.0)" 
     cmd.extent "#{width}x#{height}" 
     end 
    end 
    img = yield(img) if block_given? 
    img 
    end 
end 

답변

2

변경 :resizeprocess :resize_to_fit => [584, 329]