Permalink
Browse files

Merge pull request #31544 from mrreynolds/activestorage-combined-mini…

…magick-options

Add support for combined MiniMagick transformations
  • Loading branch information...
eileencodes committed Dec 22, 2017
2 parents 1389f4b + 7a3a991 commit 0e8ac8c44b756b33318fe69df0dcd4759c77d3ed
Showing with 30 additions and 6 deletions.
  1. +16 −6 activestorage/app/models/active_storage/variation.rb
  2. +14 −0 activestorage/test/models/variant_test.rb
@@ -46,13 +46,15 @@ def initialize(transformations)
# 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 @@ def key
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
@@ -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

0 comments on commit 0e8ac8c

Please sign in to comment.