Module: Imgproxy

Defined in:
lib/imgproxy.rb,
lib/imgproxy/config.rb,
lib/imgproxy/builder.rb,
lib/imgproxy/options.rb,
lib/imgproxy/version.rb,
lib/imgproxy/url_adapters.rb,
lib/imgproxy/extensions/shrine.rb,
lib/imgproxy/url_adapters/shrine.rb,
lib/imgproxy/url_adapters/shrine_s3.rb,
lib/imgproxy/extensions/active_storage.rb,
lib/imgproxy/url_adapters/active_storage.rb,
lib/imgproxy/url_adapters/active_storage_s3.rb,
lib/imgproxy/url_adapters/active_storage_gcs.rb

Overview

See Also:

  • ClassMethods

Defined Under Namespace

Modules: Extensions Classes: Builder, Config, Options, UrlAdapters

Constant Summary collapse

VERSION =
"1.0.5".freeze

Class Method Summary collapse

Class Method Details

.configConfig

Imgproxy config

Returns:



14
15
16
# File 'lib/imgproxy.rb', line 14

def config
  @config ||= Imgproxy::Config.new
end

.configure {|config| ... } ⇒ Config

Yields Imgproxy config

Imgproxy.configure do |config|
  config.endpoint = "http://imgproxy.example.com"
  config.hex_key = "your_key"
  config.hex_salt = "your_salt"
  config.use_short_options = true
end

Yield Parameters:

Returns:



29
30
31
32
# File 'lib/imgproxy.rb', line 29

def configure
  yield config
  config
end

.extend_active_storage!(use_s3: false, use_gcs: false, gcs_bucket: nil) ⇒ void

This method returns an undefined value.

Extends ActiveStorage::Blob with Imgproxy::Extensions::ActiveStorage#imgproxy_url method and adds URL adapters for ActiveStorage

Parameters:

  • use_s3 (Boolean) (defaults to: false)

    enable Amazon S3 source URLs

  • use_gcs (Boolean) (defaults to: false)

    enable Google Cloud Storage source URLs

  • gcs_bucket (String) (defaults to: nil)

    Google Cloud Storage bucket name



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/imgproxy.rb', line 83

def extend_active_storage!(use_s3: false, use_gcs: false, gcs_bucket: nil)
  ActiveSupport.on_load(:active_storage_blob) do
    ::ActiveStorage::Blob.include Imgproxy::Extensions::ActiveStorage

    url_adapters = Imgproxy.config.url_adapters

    url_adapters.add(Imgproxy::UrlAdapters::ActiveStorageS3.new) if use_s3
    url_adapters.add(Imgproxy::UrlAdapters::ActiveStorageGCS.new(gcs_bucket)) if use_gcs
    url_adapters.add(Imgproxy::UrlAdapters::ActiveStorage.new)
  end
end

.extend_shrine!(host: nil, use_s3: false) ⇒ void

This method returns an undefined value.

Extends Shrine::UploadedFile with Imgproxy::Extensions::Shrine#imgproxy_url method and adds URL adapters for Shrine

Parameters:

  • use_s3 (Boolean) (defaults to: false)

    enable Amazon S3 source URLs



100
101
102
103
104
105
106
107
# File 'lib/imgproxy.rb', line 100

def extend_shrine!(host: nil, use_s3: false)
  ::Shrine::UploadedFile.include Imgproxy::Extensions::Shrine

  url_adapters = Imgproxy.config.url_adapters

  url_adapters.add(Imgproxy::UrlAdapters::ShrineS3.new) if use_s3
  url_adapters.add(Imgproxy::UrlAdapters::Shrine.new(host: host))
end

.url_for(image, options = {}) ⇒ String

Genrates imgproxy URL

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  width: 500,
  height: 400,
  resizing_type: :fill,
  sharpen: 0.5
)

Parameters:

  • image (String, URI, Object)

    Source image URL or object applicable for the configured URL adapters

  • options (Hash) (defaults to: {})

    Processing options

Options Hash (options):

  • :resizing_type (String)
  • :width (Integer)
  • :height (Integer)
  • :dpr (Float)
  • :enlarge (Boolean)
  • :extend (Boolean)
  • :gravity (String)
  • :gravity_x (Float)
  • :gravity_y (Float)
  • :quality (Integer)
  • :background (Array)
  • :blur (Float)
  • :sharpen (Float)
  • :watermark_opacity (Float)
  • :watermark_position (String)
  • :watermark_x_offset (Integer)
  • :watermark_y_offset (Integer)
  • :watermark_scale (Float)
  • :preset (Array)
  • :cachebuster (String)
  • :format (String)
  • :use_short_options (Boolean)

Returns:

  • (String)

    imgproxy URL

See Also:



72
73
74
# File 'lib/imgproxy.rb', line 72

def url_for(image, options = {})
  Imgproxy::Builder.new(options).url_for(image)
end