W3cubDocs

/Ruby on Rails 7.0

class ActiveStorage::Transformers::Transformer

Parent:
Object

A Transformer applies a set of transformations to an image.

The following concrete subclasses are included in Active Storage:

  • ActiveStorage::Transformers::ImageProcessingTransformer: backed by ImageProcessing, a common interface for MiniMagick and ruby-vips

Attributes

transformations[R]

Public Class Methods

new(transformations) Show source
# File activestorage/lib/active_storage/transformers/transformer.rb, line 14
def initialize(transformations)
  @transformations = transformations
end

Public Instance Methods

transform(file, format:) { |output| ... } Show source
# File activestorage/lib/active_storage/transformers/transformer.rb, line 21
def transform(file, format:)
  output = process(file, format: format)

  begin
    yield output
  ensure
    output.close!
  end
end

Applies the transformations to the source image in file, producing a target image in the specified format. Yields an open Tempfile containing the target image. Closes and unlinks the output tempfile after yielding to the given block. Returns the result of the block.

Private Instance Methods

process(file, format:) Show source
# File activestorage/lib/active_storage/transformers/transformer.rb, line 34
def process(file, format:) # :doc:
  raise NotImplementedError
end

Returns an open Tempfile containing a transformed image in the given format. All subclasses implement this method.

© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.