Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

CHG mail image to save as png (#12923)

brozek08e3470bde3a
diff --git a/core/app/helpers/spree/images_helper.rb b/core/app/helpers/spree/images_helper.rb
index 858a072290..82e4cdefe5 100644
--- a/core/app/helpers/spree/images_helper.rb
+++ b/core/app/helpers/spree/images_helper.rb
@@ -9,7 +9,7 @@ module Spree
# @option options [Integer] :height the height of the image
def spree_image_tag(image, options = {})
image_tag(
- spree_image_url(image, { width: options[:width], height: options[:height] }),
+ spree_image_url(image, { width: options[:width], height: options[:height], format: options[:format] }),
options
)
end
@@ -18,7 +18,6 @@ module Spree
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]
@@ -28,11 +27,11 @@ module Spree
if width.present? && height.present?
url_helpers.cdn_image_url(
- image.variant(spree_image_variant_options(resize_to_fill: [width, height]))
+ 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]))
+ image.variant(spree_image_variant_options(resize_to_limit: [width, height], format: options[:format]))
)
end
end
@@ -77,16 +76,30 @@ module Spree
# @option options [Integer] :height the height of the image
def spree_image_variant_options(options = {})
{
- saver: {
- strip: true,
- quality: 75,
- lossless: false,
- alpha_q: 85,
- reduction_effort: 6,
- smart_subsample: true
- },
- format: :webp
- }.merge(options)
+ saver: options[:format] == :png ? png_variant_options : webp_variant_options,
+ format: options[:format] || :webp
+ }.merge(options.except(:format))
+ end
+
+ private
+
+ def webp_variant_options
+ {
+ strip: true,
+ quality: 75,
+ lossless: false,
+ alpha_q: 85,
+ reduction_effort: 6,
+ smart_subsample: true
+ }
+ end
+
+ def png_variant_options
+ {
+ strip: true,
+ compression_level: 8,
+ interlace: true
+ }
end
end
end
diff --git a/core/app/helpers/spree/mail_helper.rb b/core/app/helpers/spree/mail_helper.rb
index 33bff54d65..833b6e6d7b 100644
--- a/core/app/helpers/spree/mail_helper.rb
+++ b/core/app/helpers/spree/mail_helper.rb
@@ -5,7 +5,7 @@ module Spree
def variant_image_url(variant)
image = variant.default_image
- image.present? && image.attached? ? spree_image_url(image, width: 100, height: 100) : image_url('noimage/small.png')
+ image.present? && image.attached? ? spree_image_url(image, width: 100, height: 100, format: :png) : image_url('noimage/small.png')
end
def name_for(order)
diff --git a/core/spec/helpers/images_helper_spec.rb b/core/spec/helpers/images_helper_spec.rb
index a971c8a82d..e54c4dcb1a 100644
--- a/core/spec/helpers/images_helper_spec.rb
+++ b/core/spec/helpers/images_helper_spec.rb
@@ -16,7 +16,7 @@ describe Spree::ImagesHelper, type: :helper do
describe '#spree_image_tag' do
it 'returns an image tag with the correct url' do
- expect(helper).to receive(:spree_image_url).with(image, { width: 100, height: 100 }).and_return('image_url')
+ expect(helper).to receive(:spree_image_url).with(image, { width: 100, height: 100, format: nil }).and_return('image_url')
expect(helper).to receive(:image_tag).with('image_url', { width: 100, height: 100 }).and_return('image_tag')
expect(helper.spree_image_tag(image, width: 100, height: 100)).to eq('image_tag')
end
@@ -64,6 +64,15 @@ describe Spree::ImagesHelper, type: :helper do
expect(helper.spree_image_url(image, width: 100)).to eq('cdn_url')
end
end
+
+ context 'when format is provided' do
+ it 'returns a url with the correct format' do
+ variant = double('variant')
+ expect(image).to receive(:variant).with(hash_including(resize_to_fill: [200, 200], format: :png)).and_return(variant)
+ expect(Rails.application.routes.url_helpers).to receive(:cdn_image_url).and_return('cdn_url')
+ expect(helper.spree_image_url(image, width: 100, height: 100, format: :png)).to eq('cdn_url')
+ end
+ end
end
describe '#spree_asset_aspect_ratio' do
diff --git a/emails/spec/helpers/spree/mail_helper_spec.rb b/emails/spec/helpers/spree/mail_helper_spec.rb
index 260d975b6a..f2bed4537b 100644
--- a/emails/spec/helpers/spree/mail_helper_spec.rb
+++ b/emails/spec/helpers/spree/mail_helper_spec.rb
@@ -27,7 +27,7 @@ module Spree
let(:image) { create(:image) }
specify 'returns proper image path' do
- expect(subject).to eq spree_image_url(image, width: 100, height: 100)
+ expect(subject).to eq spree_image_url(image, width: 100, height: 100, format: :png)
end
end
end