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

Constants included from Alchemy::Picture::Transformations

Alchemy::Picture::Transformations::THUMBNAIL_HEIGHT, Alchemy::Picture::Transformations::THUMBNAIL_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Alchemy::Picture::Transformations

#crop, #crop_size?, #default_mask, #landscape_format?, #portrait_format?, #render_size?, #resize, #square_format?, #thumbnail_size

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)

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

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


16
17
18
# File 'app/models/alchemy/picture_variant.rb', line 16

def picture
  @picture
end

#render_formatObject (readonly)

Returns the value of attribute render_format


16
17
18
# File 'app/models/alchemy/picture_variant.rb', line 16

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


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

def image
  image = image_file

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

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