Module: Alchemy::PictureThumbnails

Extended by:
ActiveSupport::Concern
Included in:
Ingredients::Picture
Defined in:
app/models/concerns/alchemy/picture_thumbnails.rb

Overview

Picture thumbnails and cropping concerns

Instance Method Summary collapse

Instance Method Details

#allow_image_cropping?Boolean

Show image cropping link for ingredient

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'app/models/concerns/alchemy/picture_thumbnails.rb', line 104

def allow_image_cropping?
  settings[:crop] && picture &&
    picture.can_be_cropped_to?(
      settings[:size],
      settings[:upsample]
    ) && !!picture.image_file
end

#image_cropper_settingsObject

Settings for the graphical JS image cropper



92
93
94
95
96
97
98
99
100
101
# File 'app/models/concerns/alchemy/picture_thumbnails.rb', line 92

def image_cropper_settings
  Alchemy::ImageCropperSettings.new(
    render_size: dimensions_from_string(render_size.presence || settings[:size]),
    default_crop_from: default_crop_from,
    default_crop_size: default_crop_size,
    fixed_ratio: settings[:fixed_ratio],
    image_width: picture&.image_file_width,
    image_height: picture&.image_file_height
  ).to_h
end

#picture_url(options = {}) ⇒ String

The url to show the picture.

Takes all values like name and crop sizes (crop_from, crop_size from the build in graphical image cropper) and also adds the security token.

You typically want to set the size the picture should be resized to.

Example:

picture_view.picture_url(size: '200x300', crop: true, format: 'gif')
# '/pictures/1/show/200x300/crop/cats.gif?sh=765rfghj'

Parameters:

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

    a customizable set of options

Options Hash (options):

  • size (String)

    The size the picture should be resized to.

  • format (String)

    The format the picture should be rendered in. Defaults to the image_output_format from the Alchemy::Config.

  • crop (Boolean)

    If set to true the picture will be cropped to fit the size value.

Returns:

  • (String)


37
38
39
40
41
# File 'app/models/concerns/alchemy/picture_thumbnails.rb', line 37

def picture_url(options = {})
  return if picture.nil?

  picture.url(picture_url_options.merge(options)) || "missing-image.png"
end

#picture_url_optionsHashWithIndifferentAccess

Picture rendering options

Returns the default_render_format of the associated Alchemy::Picture together with the crop_from and crop_size values

Returns:

  • (HashWithIndifferentAccess)


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

def picture_url_options
  return {} if picture.nil?

  crop = !!settings[:crop]

  {
    format: picture.default_render_format,
    crop: crop,
    crop_from: crop && crop_from.presence || nil,
    crop_size: crop && crop_size.presence || nil,
    size: settings[:size]
  }.with_indifferent_access
end

#thumbnail_urlString

Returns an url for the thumbnail representation of the assigned picture

It takes cropping values into account, so it always represents the current image displayed in the frontend.

Returns:

  • (String)


69
70
71
72
73
# File 'app/models/concerns/alchemy/picture_thumbnails.rb', line 69

def thumbnail_url
  return if picture.nil?

  picture.url(thumbnail_url_options) || "alchemy/missing-image.svg"
end

#thumbnail_url_optionsHashWithIndifferentAccess

Thumbnail rendering options

Returns:

  • (HashWithIndifferentAccess)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/alchemy/picture_thumbnails.rb', line 78

def thumbnail_url_options
  crop = !!settings[:crop]

  {
    size: "160x120",
    crop: crop,
    crop_from: crop && crop_from.presence || default_crop_from&.join("x"),
    crop_size: crop && crop_size.presence || default_crop_size&.join("x"),
    flatten: true,
    format: picture&.image_file_format || "jpg"
  }
end