[ rails ] carrierwave gem, carrierwave-imageoptimizer gem

1. Mount Uploader

Mounting uploaders on different columns on ActiveRecord model.

class Song < ActiveRecord::Base
     mount_uploader :lyrics, LyricsUploader
     mount_uploader :alternative_lyrics, LyricsUploader
     mount_uploader :file, SongUploader
end
rails generate uploader LyricsUploader

app/uploaders/lyrics_uploader.rb

class LyricsUploader < CarrierWave::Uploader::Base

    # changing default directory, override the store_dir() method
    def store_dir
        'public/my/upload/directory'
    end
# Optional : in case no file has been uploaded
    def default_url(*args)
         # rails asset pipeline
         ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
    end

    # security upload
    def extension_whitelist
         %w(jpg jpeg gif png)
    end
end

USAGE on ActiveRecord model

s = Song.new
s.lyrics= params[:file] # Assign a file like this, or

# like this
File.open('somewhere') do |f|
 u.lyrics = f
end

s.save!
s.lyrics.url # => 'http://whatever/url/to/file.png'
s.lyrics.current_path # => 'path/to/file.png'
s.lyrics_identifier # => 'file.png'
s.lyrics_url # => 'http://whatever/url/to/file.png' 

————————————————————————————————————————————–

 

2. create Thumbnails

class MyUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick
    process resize_to_fit: [800, 800]

    version :thumb do
         process resize_to_fill: [200,200]
    end
end

NOTE : resize_to_fill() method is in http://www.rubydoc.info/github/jnicklas/carrierwave/CarrierWave/MiniMagick:resize_to_limit

USAGE creating Thumbnails

uploader = AvatarUploader.new
uploader.store!(my_file) # size: 1024x768

uploader.url # => '/url/to/my_file.png' # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200

Model variable to point back to uploader

NOTE : The model variable points to the instance object the uploader is attached to.

if model.is_a?(WhateverModel)

ref : https://github.com/carrierwaveuploader/carrierwave#conditional-versions

3. manipulate!

process :radial_blur => 10
def radial_blur(amount)
    manipulate! do |img|
        img.radial_blur(amount)
        img = yield(img) if block_given?
        img
    end
end

ref : http://stackoverflow.com/questions/32684196/undefined-method-manipulate-carrierwave-with-minimagick

NOTE : The MiniMagick make use of imagemagick `mogrify`.

Test in rails console

> image = MiniMagick::Image.new("/home/pong/Desktop/vertical_panorama_by_verticaldubai-d3gp1ja.jpg")
> image.gaussian_blur "0x6"

Writing own process 

ref : https://github.com/carrierwaveuploader/carrierwave

————————————————————————————————————————————–

optipng jpegoptim

include CarrierWave::ImageOptimizer
process :optimize

ref : https://github.com/jtescher/carrierwave-imageoptimizer

————————————————————————————————————————————–

AWS fog-aws gem

In an initializer,

config/initializers/carrier_wave.rb

CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',                            # required
aws_access_key_id: 'xxx',         # required
aws_secret_access_key: 'yyy', # required
region: 'eu-west-1', # optional, defaults to 'us-east-1'
host: 's3.example.com', # optional, defaults to nil
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
}

config.fog_provider = 'fog/aws' # required
config.fog_directory = 'name_of_directory' # required
config.fog_public = false # optional, defaults to true
config.fog_attributes = { 'Cache-Control' => "max-age=#{1.years.to_i}" } # optional, defaults to {}
end

CarrierWave.configure do |config|

config.whatever1 = whatever1

# Dynamic Asset Host, The asset_host config property can be assigned a proc (or anything that responds to call) for generating the host dynamically.

config.asset_host = proc do |file|
identifier = # some logic
"http://#{identifier}.cdn.rackspacecloud.com"
end

config.storage = :fog

CarrierWave.configure do |config|

config.whatever2 = whatever2

end

end

ref : https://github.com/fog/fog-aws

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s