Module: Spree::ImagesHelper

Included in:
CSV::ProductVariantPresenter, MailHelper
Defined in:
app/helpers/spree/images_helper.rb

Instance Method Summary collapse

Instance Method Details

#spree_asset_aspect_ratio(attachment) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/spree/images_helper.rb', line 39

def spree_asset_aspect_ratio(attachment)
  return unless attachment.present?
  return unless attachment.analyzed?

   = attachment.
  aspect_ratio = ['aspect_ratio'].presence

  return aspect_ratio if aspect_ratio

  width = ['width']&.to_f
  return unless width

  height = ['height']&.to_f
  return unless height
  return if height.zero?

  w = width.to_f
  h = height.to_f

  # Always return width / height, flipping if needed
  ratio = if h > w
            h / w
          elsif h < w
            w / h
          else
            # h == w, square image
            1.0
          end

  ratio.round(3)
end

#spree_image_tag(image, options = {}) ⇒ Object

render an image tag with a spree image variant it also automatically scales the width and height by 2x to look great on retina screens

Parameters:

  • image (ActiveStorage::Attachment)

    the image to render

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

    options for the image tag

Options Hash (options):

  • :width (Integer)

    the width of the image

  • :height (Integer)

    the height of the image



10
11
12
13
14
15
# File 'app/helpers/spree/images_helper.rb', line 10

def spree_image_tag(image, options = {})
  image_tag(
    spree_image_url(image, { width: options[:width], height: options[:height], format: options[:format] }),
    options
  )
end

#spree_image_url(image, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/spree/images_helper.rb', line 17

def spree_image_url(image, options = {})
  return unless image
  return unless image.variable?
  return if image.respond_to?(:attached?) && !image.attached?
  url_helpers = respond_to?(:main_app) ? main_app : Rails.application.routes.url_helpers
  width = options[:width]
  height = options[:height]

  width *= 2 if width.present?
  height *= 2 if height.present?

  if width.present? && height.present?
    url_helpers.cdn_image_url(
      image.variant(spree_image_variant_options(resize_to_fill: [width, height], format: options[:format]))
    )
  else
    url_helpers.cdn_image_url(
      image.variant(spree_image_variant_options(resize_to_limit: [width, height], format: options[:format]))
    )
  end
end

#spree_image_variant_options(options = {}) ⇒ Object

convert images to webp with some sane optimization defaults it also automatically scales the width and height by 2x to look great on retina screens

Parameters:

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

    options for the image variant

Options Hash (options):

  • :width (Integer)

    the width of the image

  • :height (Integer)

    the height of the image



77
78
79
80
81
82
# File 'app/helpers/spree/images_helper.rb', line 77

def spree_image_variant_options(options = {})
  {
    saver: options[:format] == :png ? png_variant_options : webp_variant_options,
    format: options[:format] || :webp
  }.merge(options.except(:format))
end