Sha256: 3dafcee72e1c3e2e8f4a4f5f4085ee5f5d7622f7cd5d440fe9e6030737867570

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'carrierwave'

module LadyJosephine
  class BaseUploader < ::CarrierWave::Uploader::Base
    include ::CarrierWave::MiniMagick

    storage :file

    process :auto_orient

    def strip
      manipulate! do |img|
        img.strip
        img = yield(img) if block_given?
        img
      end
    end

    def store_dir
      "storage/#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end

    # https://makandracards.com/makandra/12323-carrierwave-auto-rotate-tagged-jpegs
    def auto_orient
      manipulate! do |image|
        image.tap(&:auto_orient)
      end
    end

    def compress_to_fit(width, height, percentage = 70)
      manipulate! do |img|
        img.resize "#{width}x#{height}"
        if img.mime_type.match(/(png|jpeg|jpg)\z/i)
          img.combine_options do |c|
            c.quality(percentage.to_s)
            c.depth "8"
            c.interlace "plane"
          end
        end

        img = yield(img) if block_given?
        add_metadata(img)
        img
      end
    end

    def compress_to_fill(width, height, percentage = 70)
      manipulate! do |img|
        img.resize "#{width}x#{height}^"
        img.gravity "center"
        img.crop "#{width}x#{height}+0+0!"

        if img.mime_type.match(/(png|jpeg|jpg)\z/i)
          img.combine_options do |c|
            c.quality(percentage.to_s)
            c.depth "8"
            c.interlace "plane"
          end
        end

        img = yield(img) if block_given?
        add_metadata(img)
        img
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lady_josephine-0.6.3 app/uploader/lady_josephine/base_uploader.rb
lady_josephine-0.6.2 app/uploader/lady_josephine/base_uploader.rb
lady_josephine-0.6.1 app/uploader/lady_josephine/base_uploader.rb
lady_josephine-0.6.0 app/uploader/lady_josephine/base_uploader.rb