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
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() 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
10 11 12 13 14 15 |
# File 'app/helpers/spree/images_helper.rb', line 10 def spree_image_tag(image, = {}) image_tag( spree_image_url(image, { width: [:width], height: [:height], format: [:format] }), ) 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, = {}) 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 = [:width] height = [:height] 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
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
77 78 79 80 81 82 |
# File 'app/helpers/spree/images_helper.rb', line 77 def ( = {}) { saver: [:format] == :png ? : , format: [:format] || :webp }.merge(.except(:format)) end |