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.

Switch from mini_magick to Ruby VIPS (#11563)

Damian Legawiecc709c63c2e89
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 183f1e93c9..4da8338f11 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -27,6 +27,9 @@ run_tests: &run_tests
keys:
- spree-bundle-v10-ruby-2-7-{{ .Branch }}
- spree-bundle-v10-ruby-2-7
+ - run:
+ name: Install libvips
+ command: sudo apt-get install libvips
- run:
name: Ensure Bundle Install
command: |
@@ -53,6 +56,9 @@ run_tests_3_0: &run_tests_3_0
keys:
- spree-bundle-v10-ruby-3-0-{{ .Branch }}
- spree-bundle-v10-ruby-3-0
+ - run:
+ name: Install libvips
+ command: sudo apt-get install libvips
- run:
name: Ensure Bundle Install
command: |
@@ -79,6 +85,9 @@ jobs:
keys:
- spree-bundle-v10-ruby-2-7-{{ .Branch }}
- spree-bundle-v10-ruby-2-7
+ - run:
+ name: Install libvips
+ command: sudo apt-get install libvips
- run:
name: Bundle Install
command: |
@@ -97,6 +106,9 @@ jobs:
keys:
- spree-bundle-v10-ruby-3-0-{{ .Branch }}
- spree-bundle-v10-ruby-3-0
+ - run:
+ name: Install libvips
+ command: sudo apt-get install libvips
- run:
name: Bundle Install
command: |
@@ -197,6 +209,9 @@ jobs:
keys:
- spree-bundle-v10-ruby-3-0-{{ .Branch }}
- spree-bundle-v10-ruby-3-0
+ - run:
+ name: Install libvips
+ command: sudo apt-get install libvips
- run:
name: Ensure Bundle Install
command: |
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index f2ec0c1ce2..9272c741b0 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -61,11 +61,11 @@ module Spree
DEPRECATION
image_path ||= if current_store.logo.attached? && current_store.logo.variable?
- main_app.url_for(current_store.logo.variant(resize: '244x104>'))
+ main_app.url_for(current_store.logo.variant(resize_to_limit: [244, 104]))
elsif current_store.logo.attached? && current_store.logo.image?
main_app.url_for(current_store.logo)
else
- 'logo/spree_50.png'
+ 'logo/spree_50.png'
end
path = spree.respond_to?(:root_path) ? spree.root_path : main_app.root_path
diff --git a/core/app/models/concerns/spree/image_methods.rb b/core/app/models/concerns/spree/image_methods.rb
index 439e0e2f55..9d02132c8e 100644
--- a/core/app/models/concerns/spree/image_methods.rb
+++ b/core/app/models/concerns/spree/image_methods.rb
@@ -2,19 +2,21 @@ module Spree
module ImageMethods
extend ActiveSupport::Concern
- def generate_url(size:, gravity: 'center', quality: 80, background: 'show')
+ def generate_url(size:, gravity: 'centre', quality: 80, background: [0, 0, 0])
return if size.blank?
+
size = size.gsub(/\s+/, '')
return unless size.match(/(\d+)x(\d+)/)
- polymorphic_path(attachment.variant(
- gravity: gravity,
- resize: size,
- extent: size,
- background: background,
- quality: quality.to_i
- ), only_path: true)
+ width, height = size.split('x').map(&:to_i)
+
+ # FIXME: bring back support for background color
+
+ polymorphic_path(
+ attachment.variant(resize_and_pad: [width, height, { gravity: gravity }], saver: { quality: quality }),
+ only_path: true
+ )
end
def original_url
diff --git a/core/app/models/spree/asset/support/active_storage.rb b/core/app/models/spree/asset/support/active_storage.rb
index c67f315d91..619a1f7b7d 100644
--- a/core/app/models/spree/asset/support/active_storage.rb
+++ b/core/app/models/spree/asset/support/active_storage.rb
@@ -8,7 +8,7 @@ module Spree
def url(style)
return placeholder(style) unless attachment.attached?
- attachment.variant(resize: dimensions_for_style(style))
+ attachment.variant(resize_to_limit: dimensions_for_style(style))
end
def placeholder(style)
@@ -16,7 +16,8 @@ module Spree
end
def dimensions_for_style(style)
- self.class.styles.with_indifferent_access[style] || self.class.styles.with_indifferent_access[default_style]
+ dimensions = self.class.styles.with_indifferent_access[style] || self.class.styles.with_indifferent_access[default_style]
+ dimensions.split('x').map(&:to_i)
end
end
end
diff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rb
index bb858a2306..c64dcfa566 100644
--- a/core/app/models/spree/cms_section.rb
+++ b/core/app/models/spree/cms_section.rb
@@ -18,7 +18,7 @@ module Spree
define_method("img_#{count}_#{size}") do |dimensions = nil|
return if !send("image_#{count}").attached? || dimensions.nil?
- send("image_#{count}").variant(resize: dimensions)
+ send("image_#{count}").variant(resize_to_limit: dimensions.split('x').map(&:to_i))
end
end
end
diff --git a/core/app/models/spree/image.rb b/core/app/models/spree/image.rb
index fe47d98598..158fb0a213 100644
--- a/core/app/models/spree/image.rb
+++ b/core/app/models/spree/image.rb
@@ -13,7 +13,7 @@ module Spree
def styles
self.class.styles.map do |_, size|
- width, height = size.chop.split('x')
+ width, height = size.chop.split('x').map(&:to_i)
{
url: generate_url(size: size),
@@ -28,7 +28,7 @@ module Spree
size = self.class.styles[name]
return unless size
- width, height = size.chop.split('x')
+ width, height = size.chop.split('x').map(&:to_i)
{
url: generate_url(size: size),
@@ -40,7 +40,7 @@ module Spree
def style_dimensions(name)
size = self.class.styles[name]
- width, height = size.chop.split('x')
+ width, height = size.chop.split('x').map(&:to_i)
{
width: width,
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 6fa40d529f..80b3a75119 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -185,7 +185,7 @@ module Spree
def favicon
return unless favicon_image.attached?
- favicon_image.variant(resize: '32x32')
+ favicon_image.variant(resize_to_limit: [32, 32])
end
def can_be_deleted?
diff --git a/core/app/models/spree/taxon_image.rb b/core/app/models/spree/taxon_image.rb
index 5602966f71..60ab005b45 100644
--- a/core/app/models/spree/taxon_image.rb
+++ b/core/app/models/spree/taxon_image.rb
@@ -6,10 +6,10 @@ module Spree
def styles
self.class.styles.map do |_, size|
- width, height = size[/(\d+)x(\d+)/].split('x')
+ width, height = size[/(\d+)x(\d+)/].split('x').map(&:to_i)
{
- url: polymorphic_path(attachment.variant(resize: size), only_path: true),
+ url: polymorphic_path(attachment.variant(resize_to_limit: [width, height]), only_path: true),
width: width,
height: height
}
diff --git a/core/config/initializers/active_storage.rb b/core/config/initializers/active_storage.rb
index 5776bc935a..29d15e378d 100644
--- a/core/config/initializers/active_storage.rb
+++ b/core/config/initializers/active_storage.rb
@@ -1,2 +1,2 @@
Rails.application.config.active_storage.content_types_to_serve_as_binary.delete('image/svg+xml')
-Rails.application.config.active_storage.variant_processor = :mini_magick
+Rails.application.config.active_storage.variant_processor = :vips
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 65800ec727..e35cbef8ce 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -14,7 +14,7 @@ require 'friendly_id'
require 'kaminari'
require 'monetize'
require 'paranoia'
-require 'mini_magick'
+require 'ruby-vips'
require 'ransack'
require 'state_machines-activerecord'
require 'active_storage_validations'
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index aaa480adc2..468bedea62 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -608,7 +608,7 @@ describe Spree::Store, type: :model do
it 'returns a resized favicon' do
expect(favicon_variation).to be_present
- expect(favicon_variation.transformations.fetch(:resize)).to eq('32x32')
+ expect(favicon_variation.transformations.fetch(:resize_to_limit)).to eq([32, 32])
end
end
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index 5adeeddb8d..e0a7029ada 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -51,7 +51,6 @@ Gem::Specification.new do |s|
s.add_dependency 'state_machines-activemodel', '~> 0.7'
s.add_dependency 'stringex'
s.add_dependency 'validates_zipcode'
- s.add_dependency 'mini_magick', '~> 4.9', '>= 4.9.4'
s.add_dependency 'image_processing', '~> 1.2'
s.add_dependency 'active_storage_validations', '~> 0.9', '<= 0.9.5'
s.add_dependency 'activerecord-typedstore'
diff --git a/emails/app/helpers/spree/mail_helper.rb b/emails/app/helpers/spree/mail_helper.rb
index 9860b2b96c..1dae324ca3 100644
--- a/emails/app/helpers/spree/mail_helper.rb
+++ b/emails/app/helpers/spree/mail_helper.rb
@@ -26,7 +26,7 @@ module Spree
def logo_path
return default_logo unless store_logo.attached?
- return main_app.url_for(store_logo.variant(resize: '244x104>')) if store_logo.variable?
+ return main_app.url_for(store_logo.variant(resize_to_limit: [244, 104])) if store_logo.variable?
return main_app.url_for(store_logo) if store_logo.image?
end