Search code examples
ruby-on-railsrubyruby-on-rails-4carrierwaveminimagick

Rails4 - How to compress images attached by users in Rails app without cropping?


I am using redactor gem in my Rails app which is WYSIWYT editor. It allows users to attach images.

I have this file under uploaders folder which attaches images to the posts made by using the redactor gem.

redactor_rails_picture_uploader.rb

# encoding: utf-8
class RedactorRailsPictureUploader < CarrierWave::Uploader::Base
  include RedactorRails::Backend::CarrierWave

  # Include RMagick or ImageScience support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  # include CarrierWave::ImageScience

  # Choose what kind of storage to use for this uploader:
  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
    "system/redactor_assets/pictures/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  process :read_dimensions

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fill => [118, 100]
  # end

  # version :content do
  #   process :resize_to_limit => [800, 800]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    RedactorRails.image_file_types
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end
end

I do not want to restrict users by saying them to upload images less then 10 or 8 MB.
I also do not want to crop the images.
I would like to compress images to less than 10 or 8 MB in my rails app and not bother users to crop or compress images.

I have gems like carrierwave, mini_magick and they all work. This is my gem file.

source 'https://rubygems.org'
ruby   '2.1.4'

gem 'bootstrap-sass', '~> 3.3.1'
gem 'autoprefixer-rails'

gem 'rails', '4.2.0.beta2'
gem 'bcrypt', '3.1.7'
gem 'faker', '1.4.2'
gem 'carrierwave',             '0.10.0'
gem 'mini_magick',             '3.8.0'
gem 'paperclip'
gem 'aws-sdk'
gem 'fog',                     '1.23.0'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'

gem 'mailboxer'
gem 'rack-reverse-proxy', :require => 'rack/reverse_proxy'

gem 'chosen-rails'
gem 'jquery-turbolinks'

gem 'masonry-rails', '~> 0.2.4'

gem 'newrelic_rpm'

gem 'rocket_pants', '~> 1.10.0'

gem "recaptcha", :require => "recaptcha/rails"

gem 'metamagic'

gem 'redactor-rails'

gem 'fitvidsjs_rails'

gem 'pusher'

gem 'sass-rails', '~> 5.0.0.beta1'

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails', '~> 4.0.0.beta2'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc


group :development, :test do

  gem 'byebug'

  gem 'web-console', '~> 2.0.0.beta4'

  gem 'spring'

  gem 'sqlite3'  
end

group :production do
  gem 'pg', '0.17.1'
  gem 'rails_12factor', '0.0.2'
  gem 'unicorn'
end

carrier_wave.rb

if Rails.env.production?
  CarrierWave.configure do |config|
    config.fog_credentials = {
      # Configuration for Amazon S3
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
      :aws_secret_access_key => ENV['S3_SECRET_KEY'],
      :region                => ENV['S3_REGION'],
      :path_style            => true
    }
    config.fog_directory     =  ENV['S3_BUCKET']
  end
end

Could anyone share how to do this?
Kindly help. Thanks.


Solution

  • I believe this would answer your question:

    How To Specify the Image Quality in carrierwave.

    I don't know how to specify the megabyte, but you could maybe do some on-the-fly calculations about how big the file is and what to set the quality to. Bigger file means lower quality.

    Hope this helps :)


    Copy-paste from the page

    If you want to set the image quality, create a file config/initializers/carrierwave.rb with the following content:

    module CarrierWave
      module RMagick
    
        def quality(percentage)
          manipulate! do |img|
            img.write(current_path){ self.quality = percentage } unless img.quality == percentage
            img = yield(img) if block_given?
            img
          end
        end
    
      end
    end
    

    Now you can use the process :quality in your uploaders:

    class AvatarUploader < CarrierWave::Uploader::Base
      include CarrierWave::RMagick
    
      version :thumb do
        process :resize_to_fit => [100, 100]
        process :quality => 100 
      end
    end
    

    Change quality with MiniMagick

    # put this in config/initializers/carrierwave.rb
    module CarrierWave
      module MiniMagick
        def quality(percentage)
          manipulate! do |img|
            img.quality(percentage.to_s)
            img = yield(img) if block_given?
            img
          end
        end
      end
    end
    

    In your uploader class

    class ImageUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
    
      process :quality => 85
    
      version :medium do
        process :quality => 85
      end
    end