Class: Alchemy::PictureVariant

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logger, Alchemy::Picture::Transformations
Defined in:
app/models/alchemy/picture_variant.rb

Overview

Represents a rendered picture

Resizes, crops and encodes the image with imagemagick

Constant Summary collapse

ANIMATED_IMAGE_FORMATS =
%w[gif webp]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Alchemy::Picture::Transformations

#crop, #crop_size?, #render_size?, #resize

Methods included from Logger

#log_warning, warn

Constructor Details

#initialize(picture, options = {}) ⇒ PictureVariant

Returns a new instance of PictureVariant.

Parameters:

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

    passed to the image processor

Options Hash (options):

  • :crop (Boolean)

    Pass true to enable cropping

  • :crop_from (String)

    Coordinates to start cropping from

  • :crop_size (String)

    Size of the cropping area

  • :flatten (Boolean)

    Pass true to flatten GIFs

  • :format (String|Symbol)

    Image format to encode the image in

  • :quality (Integer)

    JPEG compress quality

  • :size (String)

    Size of resulting image in WxH

  • :upsample (Boolean)

    Pass true to upsample (grow) an image if the original size is lower than the resulting size

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'app/models/alchemy/picture_variant.rb', line 39

def initialize(picture, options = {})
  raise ArgumentError, "Picture missing!" if picture.nil?

  @picture = picture
  @options = options
  @render_format = options[:format] || picture.default_render_format
end

Instance Attribute Details

#pictureObject (readonly)

Returns the value of attribute picture.



18
19
20
# File 'app/models/alchemy/picture_variant.rb', line 18

def picture
  @picture
end

#render_formatObject (readonly)

Returns the value of attribute render_format.



18
19
20
# File 'app/models/alchemy/picture_variant.rb', line 18

def render_format
  @render_format
end

Instance Method Details

#imageDragonfly::Attachment|Dragonfly::Job

Process a variant of picture

Returns:

  • (Dragonfly::Attachment|Dragonfly::Job)

    The processed image variant



51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/alchemy/picture_variant.rb', line 51

def image
  image = image_file

  raise MissingImageFileError, "Missing image file for #{picture.inspect}" if image.nil?

  image = processed_image(image, @options)
  encoded_image(image, @options)
rescue MissingImageFileError, WrongImageFormatError => e
  log_warning(e.message)
  nil
end