A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/imgproxy/imgproxy.rb below:

imgproxy/imgproxy.rb: Framework-agnostic Ruby Gem for imgproxy with support for Ruby on Rails' most popular image attachment options

Website | Blog | Documentation | imgproxy Pro

imgproxy is a fast and secure standalone server for resizing and converting remote images. The main principles of imgproxy are simplicity, speed, and security. It is a Go application, ready to be installed and used in any Unix environment—also ready to be containerized using Docker.

imgproxy can be used to provide a fast and secure way to get rid of all the image resizing code in your web application (like calling ImageMagick or GraphicsMagick, or using libraries), while also being able to resize everything on the fly on a separate server that only you control. imgproxy is fast, easy to use, and requires zero processing power or storage from the main application. imgproxy is indispensable when handling image resizing of epic proportions, especially when original images are coming from a remote source.

imgproxy.rb is a framework-agnostic Ruby Gem for imgproxy that includes proper support for Ruby on Rails' most popular image attachment options: Active Storage and Shrine.

Important

This readme shows documentation for version 3.x.

See Upgrading imgproxy.rb for the upgrade guide.

Add this to your Gemfile:

or install system-wide:

imgproxy.rb uses anyway_config to load configuration, so you can configure it in different ways.

With a separate config file:

# <Rails root>/config/imgproxy.yml
development:
  # Full URL to where your imgproxy lives.
  endpoint: "http://imgproxy.example.com"
  # Hex-encoded signature key and salt
  key: "your_key"
  salt: "your_salt"
production: ...
test: ...

With a secrets.yml entry for imgproxy:

# secrets.yml
production:
  ...
  imgproxy:
    # Full URL to where your imgproxy lives.
    endpoint: "http://imgproxy.example.com"
    # Hex-encoded signature key and salt
    key: "your_key"
    salt: "your_salt"
...

With environment variables:

IMGPROXY_ENDPOINT="http://imgproxy.example.com" \
IMGPROXY_KEY="your_key" \
IMGPROXY_SALT="your_salt" \
rails s

...or right in your application code:

# config/initializers/imgproxy.rb

Imgproxy.configure do |config|
  # Full URL to where your imgproxy lives.
  config.endpoint = "http://imgproxy.example.com"
  # Hex-encoded signature key and salt
  config.key = "your_key"
  config.salt = "your_salt"
end
Using with Active Storage

imgproxy.rb comes with the Active Storage support built-in. It is enabled automagically if you load imgproxy gem after rails (basically, just put gem "imgproxy" after gem "rails" in your Gemfile). Otherwise, modify your initializer at config/initializers/imgproxy.rb:

# config/initializers/imgproxy.rb

Imgproxy.extend_active_storage!

Now, to add imgproxy processing to your image attachments, just use the imgproxy_url method:

user.avatar.imgproxy_url(width: 500, height: 400, resizing_type: :fill)

This method will return a URL to your user's avatar, resized to fill 500x400px on the fly.

If you're a happy user of imgproxy Pro, you may find useful it's Getting an image info feature. imgproxy.rb allows you to easily generate info URLs for your images:

user.avatar.imgproxy_info_url(detect_objects: true, palette: 128)

This method will return a URL to the JSON with the requested info about your user's avatar.

If you have configured both your imgproxy server and Active Storage to work with Amazon S3, you can use use_s3_urls config option (or IMGPROXY_USE_S3_URLS env variable) to make imgproxy.rb use short s3://... source URLs instead of long ones generated by Rails.

You can also enable gs://... URLs usage for the files stored in Google Cloud Storage with use_gcs_urls and gcs_bucket config options (or IMGPROXY_USE_GCS_URLS and IMGPROXY_GCS_BUCKET env variables).

Important

You need to explicitly provide GCS bucket name since Active Storage "hides" the GCS config.

You can also use imgproxy.rb's built-in Shrine support. It is enabled automagically if you load imgproxy gem after shrine (basically, just put gem "imgproxy" after gem "shrine" in your Gemfile). Otherwise, modify your initializer at config/initializers/imgproxy.rb:

# config/initializers/imgproxy.rb

Imgproxy.extend_shrine!

Now you can use imgproxy_url method of Shrine::UploadedFile:

user.avatar.imgproxy_url(width: 500, height: 400, resizing_type: :fill)

This method will return a URL to your user's avatar, resized to fill 500x400px on the fly.

If you're a happy user of imgproxy Pro, you may find useful it's Getting an image info feature. imgproxy.rb allows you to easily generate info URLs for your images:

user.avatar.imgproxy_info_url(detect_objects: true, palette: 128)

This method will return a URL to the JSON with the requested info about your user's avatar.

Important

If you use Shrine::Storage::FileSystem as storage, uploaded file URLs won't include the hostname, so imgproxy server won't be able to access them. To fix this, use shrine_host config.

Alternatively, you can launch your imgproxy server with the IMGPROXY_BASE_URL setting:

IMGPROXY_BASE_URL="http://your-host.test" imgproxy

If you have configured both your imgproxy server and Shrine to work with Amazon S3, you can use use_s3_urls config option (or IMGPROXY_USE_S3_URLS env variable) to make imgproxy.rb use short s3://... source URLs instead of long ones generated by Shrine.

Usage imgproxy.rb in a framework-agnostic way

If you use another gem for your attachment operations, you like to keep things minimal or Rails-free, or if you want to generate imgproxy URLs for pictures that did not originate from your application, you can use the Imgproxy.url_for method:

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

This method will return a URL to the image, resized to fill 500x400px on the fly.

If you're a happy user of imgproxy Pro, you may find useful it's Getting an image info feature. imgproxy.rb allows you to easily generate info URLs for your images:

Imgproxy.info_url_for(
  "http://images.example.com/images/image.jpg",
  detect_objects: true,
  palette: 128
)

This method will return a URL to the JSON with the requested info about the image.

You can reuse processing options by using Imgproxy::Builder:

builder = Imgproxy::UrlBuilders::Processing.new(
  width: 500,
  height: 400,
  resizing_type: :fill,
  sharpen: 0.5
)

builder.url_for("http://images.example.com/images/image1.jpg")
builder.url_for("http://images.example.com/images/image2.jpg")

info_builder = Imgproxy::UrlBuilders::Info.new(
  detect_objects: true,
  palette: 128
)

info_builder.url_for("http://images.example.com/images/image1.jpg")
info_builder.url_for("http://images.example.com/images/image2.jpg")
Supported imgproxy options

See Supported processing options for the supported processing options list and their arguments.

See Supported info options for the supported info options list and their arguments.

Complex processing options

Some of the processing and info options like crop or gravity may have multiple arguments, and you can define these arguments multiple ways:

First and the most readable way is to use a Hash with named arguments:

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  crop: {
    width: 500,
    height: 600,
    gravity: {
      type: :nowe,
      x_offset: 10,
      y_offset: 5
    }
  }
)
# => .../c:500:600:nowe:10:5/...

You can find argument names on the Supported processing options and Supported info options pages.

Using named arguments with usupported options

You can use named arguments even if the option is not supported by the gem. In this case the arguments won't be reordered nor formatted, so you should provide them in the same order and right the same way they should appear in the URL:

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  unsupported: {
    arg1: 1,
    nested1: {
      arg2: 2,
      nested2: {
        arg3: 3
      }
    }
  }
)
# => .../unsupported:1:2:3/...

The arguments of the complex options can be provided as an array of formatted values:

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  crop: [500, 600, :nowe, 10, 5],
  trim: [10, "aabbcc", 1. 1]
)
# => .../c:500:600:nowe:10:5/t:10:aabbcc:1:1/...

If a complex option has a single required argument, and you don't want to use the optional ones, you can just use the required argument value:

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  gravity: :nowe,
  trim: 10
)
# => .../g:nowe/t:10/...
Base64 processing options arguments

Some of the processing options like watermark_url or style require their arguments to be base64-encoded. Good news is that imgproxy.rb will encode them for you:

Imgproxy.url_for(
  "http://images.example.com/images/image.jpg",
  watermark_url: "http://example.com/watermark.jpg",
  style: "color: rgba(255, 255, 255, .5)"
)
# => .../wmu:aHR0cDovL2V4YW1wbGUuY29tL3dhdGVybWFyay5qcGc/st:Y29sb3I6IHJnYmEoMjU1LCAyNTUsIDI1NSwgLjUp/...

By default, Imgproxy.url_for accepts only String and URI as the source URL, but you can extend that behavior by using URL adapters.

Tip

imgproxy.rb provides built-in adapters for Active Storage and Shrine that are automatically added when Active Storage or Shrine support is enabled.

URL adapter is a simple class that implements applicable? and url methods. See the example below:

class MyItemAdapter
  # `applicable?` checks if the adapter can extract
  # source URL from the provided object
  def applicable?(item)
    item.is_a? MyItem
  end

  # `url` extracts source URL from the provided object
  def url(item)
    item.image_url
  end
end

# ...

Imgproxy.configure do |config|
  config.url_adapters.add MyItemAdapter.new
end

Note

Imgproxy will use the first applicable URL adapter. If you need to add your adapter to the beginning of the list, use the prepend method instead of add.

If you use more than one instance of imgproxy and they have different endpoints and key/salt configurations you can specify them in services option.

Imgproxy.configure do |config|
  config.endpoint = "https://main.imgproxy.com/"
  config.service(:pro) do |pro|
    pro.endpoint = "https://pro.imgproxy.com/"
    pro.key = ENV["IMGPROXY_PRO_KEY"]
    pro.salt = ENV["IMGPROXY_PRO_SALT"]
    pro.source_url_encryption_key = ENV["IMGPROXY_PRO_ENCRYPTION_KEY"]
    pro.always_encrypt_source_urls = true
  end
end

Or via YAML config:

endpoint: "https://main.imgproxy.com/"
services:
  pro:
    endpoint: "https://pro.imgproxy.com/"
    key: <%= ENV["IMGPROXY_PRO_KEY"] %>
    salt: <%= ENV["IMGPROXY_PRO_SALT"] %>
    source_url_encryption_key: ENV["IMGPROXY_PRO_ENCRYPTION_KEY"]
    always_encrypt_source_urls: true

If you don't specify key, salt, endpoint, signature_size, source_url_encryption_key, or always_encrypt_source_urls, they are inherited from the global configuration.

Pass the service option to url_for and info_url_for:

Imgproxy.url_for(image, service: :pro)
Imgproxy.info_url_for(image, service: :pro)

Bug reports and pull requests are welcome on GitHub at https://github.com/imgproxy/imgproxy.rb.

If you are having any problems with image processing of imgproxy itself, be sure to visit https://github.com/imgproxy/imgproxy first and check out the docs at https://docs.imgproxy.net/.

The gem is available as open source under the terms of the MIT License.

To report a security vulnerability, please contact us at security@imgproxy.net. We will coordinate the fix and disclosure.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4