Module: Spree::ImagesHelper
- Included in:
- CSV::ProductVariantPresenter, MailHelper
- Defined in:
- app/helpers/spree/images_helper.rb
Instance Method Summary collapse
- #spree_asset_aspect_ratio(attachment) ⇒ Object
-
#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.
- #spree_image_url(image, options = {}) ⇒ Object
-
#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.
Instance Method Details
#spree_asset_aspect_ratio(attachment) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/helpers/spree/images_helper.rb', line 59 def spree_asset_aspect_ratio() return unless .present? return unless .analyzed? = . 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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/helpers/spree/images_helper.rb', line 11 def spree_image_tag(image, = {}) return unless image return unless image.variable? return if image.respond_to?(:attached?) && !image.attached? = if [:variant].present? { variant: [:variant] } else { width: [:width], height: [:height], format: [:format] } end image_tag( spree_image_url(image, ), .except(:variant, :format) ) end |
#spree_image_url(image, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/helpers/spree/images_helper.rb', line 28 def spree_image_url(image, = {}) 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 # Use preprocessed named variant if specified (e.g., :mini, :small, :medium, :large, :xlarge) if [:variant].present? return url_helpers.cdn_image_url(image.variant([:variant])) end # Dynamic variant generation for width/height options width = [:width] height = [:height] # Double dimensions for retina displays width *= 2 if width.present? height *= 2 if height.present? if width.present? && height.present? url_helpers.cdn_image_url( image.variant((resize_to_fill: [width, height], format: [:format])) ) else url_helpers.cdn_image_url( image.variant((resize_to_limit: [width, height], format: [:format])) ) end end |
#spree_image_variant_options(options = {}) ⇒ Object
The key order matters for variation digest matching with preprocessed variants. Active Storage reorders keys alphabetically, so use: format, resize_to_fill/limit, saver
Use string values (not symbols) for format because variation keys are JSON-encoded in URLs and JSON converts symbols to strings, causing digest mismatches.
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
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/helpers/spree/images_helper.rb', line 102 def ( = {}) format_opt = [:format]&.to_s = format_opt == "png" ? : Spree::Asset::WEBP_SAVER_OPTIONS format = format_opt || "webp" # Build hash in alphabetical order to match Active Storage's key ordering result = {} result[:format] = format result[:resize_to_fill] = [:resize_to_fill] if [:resize_to_fill] result[:resize_to_limit] = [:resize_to_limit] if [:resize_to_limit] result[:saver] = result end |