Domanda

I have looked around and could not find a solution after I tried to do it on my own. When users upload photos, I want for them to be resized if it exceeds my minimum and max dimensions. However I would like two conditionals. The photos that were taken sideways (East/West) should remain within the dimensions I set, and the same follows for the photos taken the tall way (North/South).

For example is a user uploads a photo that's standing the long way and has dimensions of 3264x1840. The upload should be resized to fit 584x329. If the upload is less than 584x329 then it will not adjust the size.

The other example is if user uploads a photo that was taken the tall way and it had dimensions of 2448 x 3264. The upload should be resized to fit 247x329.

I was trying to use MiniMagick with this as I believe that would be the requirement. If I can only use CarrierWave then that's perfect, but I thought MiniMagick was supposed to be used for resizing photos.

The error I receive is 'undefined method resize' for #<ImageUploader:0x007f8606feb9b8>' and it points to@photo = Photo.new(params[:photo])` from def create in the controller.

BTW dimensions are high because those are normally your phones default sizes when you upload a photo.

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

Photos Controller:

  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
È stato utile?

Soluzione

Change :resize in the image uploader to process :resize_to_fit => [584, 329]

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top