123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Transformers::ImageProcessingTransformer

Relationships & Source Files
Namespace Children
Exceptions:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Transformer
Instance Chain:
self, Transformer
Inherits: ActiveStorage::Transformers::Transformer
Defined in: activestorage/lib/active_storage/transformers/image_processing_transformer.rb

Class Method Summary

Transformer - Inherited

Instance Attribute Summary

Instance Method Summary

Transformer - Inherited

#transform

Applies the transformations to the source image in file, producing a target image in the specified format.

#process

Returns an open Tempfile containing a transformed image in the given format.

Constructor Details

This class inherits a constructor from ActiveStorage::Transformers::Transformer

Instance Method Details

#operations

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 32

def operations
  transformations.each_with_object([]) do |(name, argument), list|
    if ActiveStorage.variant_processor == :mini_magick
      validate_transformation(name, argument)
    end

    if name.to_s == "combine_options"
      raise ArgumentError, <<~ERROR.squish
        Active Storage's ImageProcessing transformer doesn't support :combine_options,
        as it always generates a single ImageMagick command.
      ERROR
    end

    if argument.present?
      list << [ name, argument ]
    end
  end
end

#process(file, format:)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 19

def process(file, format:)
  processor.
    source(file).
    loader(page: 0).
    convert(format).
    apply(operations).
    call
end

#processor

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 28

def processor
  ImageProcessing.const_get(ActiveStorage.variant_processor.to_s.camelize)
end

#validate_arg_array(argument)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 79

def validate_arg_array(argument)
  argument.each do |arg|
    if arg.is_a?(Integer) || arg.is_a?(Float)
      next
    elsif arg.is_a?(String) || arg.is_a?(Symbol)
      validate_arg_string(arg)
    elsif arg.is_a?(Array)
      validate_arg_array(arg)
    elsif arg.is_a?(Hash)
      validate_arg_hash(arg)
    end
  end
end

#validate_arg_hash(argument)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 93

def validate_arg_hash(argument)
  argument.each do |key, value|
    validate_arg_string(key)

    if value.is_a?(Integer) || value.is_a?(Float)
      next
    elsif value.is_a?(String) || value.is_a?(Symbol)
      validate_arg_string(value)
    elsif value.is_a?(Array)
      validate_arg_array(value)
    elsif value.is_a?(Hash)
      validate_arg_hash(value)
    end
  end
end

#validate_arg_string(argument)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 71

def validate_arg_string(argument)
  unsupported_arguments = ActiveStorage.unsupported_image_processing_arguments.any? do |bad_arg|
    argument.to_s.downcase.include?(bad_arg)
  end

  raise UnsupportedImageProcessingArgument if unsupported_arguments
end

#validate_transformation(name, argument)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/transformers/image_processing_transformer.rb', line 51

def validate_transformation(name, argument)
  method_name = name.to_s.tr("-", "_")

  unless ActiveStorage.supported_image_processing_methods.any? { |method| method_name == method }
    raise UnsupportedImageProcessingMethod, <<~ERROR.squish
      One or more of the provided transformation methods is not supported.
    ERROR
  end

  if argument.present?
    if argument.is_a?(String) || argument.is_a?(Symbol)
      validate_arg_string(argument)
    elsif argument.is_a?(Array)
      validate_arg_array(argument)
    elsif argument.is_a?(Hash)
      validate_arg_hash(argument)
    end
  end
end