aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorRobert Glaser <mail@robert-glaser.de>2017-12-22 16:44:34 +0100
committerRobert Glaser <mail@robert-glaser.de>2017-12-22 16:44:34 +0100
commit7a3a991f6d763e7871cbddda2f1e6e41e6c4c6b8 (patch)
tree81b6c9f64e4f7ea79cd47a46233f74892eaea0d2 /activestorage
parent58ff921c1d8e00c4a7b6ab47df4343c101e3276a (diff)
downloadrails-7a3a991f6d763e7871cbddda2f1e6e41e6c4c6b8.tar.gz
rails-7a3a991f6d763e7871cbddda2f1e6e41e6c4c6b8.tar.bz2
rails-7a3a991f6d763e7871cbddda2f1e6e41e6c4c6b8.zip
Add support for combined MiniMagick transformations
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/app/models/active_storage/variation.rb22
-rw-r--r--activestorage/test/models/variant_test.rb14
2 files changed, 30 insertions, 6 deletions
diff --git a/activestorage/app/models/active_storage/variation.rb b/activestorage/app/models/active_storage/variation.rb
index fc4305dcc5..09997eb97f 100644
--- a/activestorage/app/models/active_storage/variation.rb
+++ b/activestorage/app/models/active_storage/variation.rb
@@ -46,13 +46,15 @@ class ActiveStorage::Variation
# Accepts an open MiniMagick image instance, like what's returned by <tt>MiniMagick::Image.read(io)</tt>,
# and performs the +transformations+ against it. The transformed image instance is then returned.
def transform(image)
- transformations.each do |method, argument|
- image.mogrify do |command|
- if eligible_argument?(argument)
- command.public_send(method, argument)
- else
- command.public_send(method)
+ transformations.each do |(method, argument)|
+ if method.to_s == "combine_options"
+ image.combine_options do |combination|
+ argument.each do |(method, argument)|
+ pass_transform_argument(combination, method, argument)
+ end
end
+ else
+ pass_transform_argument(image, method, argument)
end
end
end
@@ -66,4 +68,12 @@ class ActiveStorage::Variation
def eligible_argument?(argument)
argument.present? && argument != true
end
+
+ def pass_transform_argument(instance, method, argument)
+ if eligible_argument?(argument)
+ instance.public_send(method, argument)
+ else
+ instance.public_send(method)
+ end
+ end
end
diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb
index 8eced41ee0..2a96dfb142 100644
--- a/activestorage/test/models/variant_test.rb
+++ b/activestorage/test/models/variant_test.rb
@@ -25,6 +25,20 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
assert_match(/Gray/, image.colorspace)
end
+ test "center-weighted crop of JPEG blob" do
+ blob = create_file_blob(filename: "racecar.jpg")
+ variant = blob.variant(combine_options: {
+ gravity: "center",
+ resize: "100x100^",
+ crop: "100x100+0+0",
+ }).processed
+ assert_match(/racecar\.jpg/, variant.service_url)
+
+ image = read_image(variant)
+ assert_equal 100, image.width
+ assert_equal 100, image.height
+ end
+
test "resized variation of PSD blob" do
blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop")
variant = blob.variant(resize: "20x20").processed