Skip to content

Commit

Permalink
Use a shim to simplify support for minimagick 4 and 5
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeasrodgers committed Nov 16, 2024
1 parent f7510a1 commit 129b949
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
23 changes: 12 additions & 11 deletions lib/image_processing/mini_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ module ImageProcessing
module MiniMagick
extend Chainable

# Returns whether the given image file is processable.
def self.valid_image?(file)
def self.convert_shim(&block)
if ::MiniMagick.respond_to?(:convert)
::MiniMagick.convert do |convert|
convert << file.path
convert << "null:"
end
::MiniMagick.convert(&block)
else
::MiniMagick::Tool::Convert.new do |convert|
convert << file.path
convert << "null:"
end
::MiniMagick::Tool::Convert.new(&block)
end
end

# Returns whether the given image file is processable.
def self.valid_image?(file)
convert_shim do |convert|
convert << file.path
convert << "null:"
end
true
rescue ::MiniMagick::Error
Expand All @@ -37,7 +38,7 @@ def self.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_
magick = path_or_magick
else
source_path = path_or_magick
magick = ::MiniMagick::Tool::Convert.new
magick = ::ImageProcessing::MiniMagick.convert_shim

Utils.apply_options(magick, **options)

Expand Down
12 changes: 6 additions & 6 deletions test/mini_magick_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
it "applies imagemagick operations" do
actual = ImageProcessing::MiniMagick.flip.call(@portrait)
expected = Tempfile.new(["result", ".jpg"], binmode: true).tap do |tempfile|
MiniMagick::Tool::Convert.new do |cmd|
ImageProcessing::MiniMagick.convert_shim do |cmd|
cmd << @portrait.path
cmd.flip
cmd << tempfile.path
Expand All @@ -37,7 +37,7 @@
it "applies macro operations" do
actual = ImageProcessing::MiniMagick.resize_to_limit(400, 400).call(@portrait)
expected = Tempfile.new(["result", ".jpg"], binmode: true).tap do |tempfile|
MiniMagick::Tool::Convert.new do |cmd|
ImageProcessing::MiniMagick.convert_shim do |cmd|
cmd << @portrait.path
cmd.resize("400x400")
cmd << tempfile.path
Expand All @@ -55,7 +55,7 @@

it "accepts page" do
tiff = Tempfile.new(["file", ".tiff"])
MiniMagick::Tool::Convert.new do |convert|
ImageProcessing::MiniMagick.convert_shim do |convert|
convert.merge! [@portrait.path, @portrait.path, @portrait.path]
convert << tiff.path
end
Expand All @@ -70,7 +70,7 @@

it "disallows split layers by default" do
tiff = Tempfile.new(["file", ".tiff"])
MiniMagick::Tool::Convert.new do |convert|
ImageProcessing::MiniMagick.convert_shim do |convert|
convert.merge! [@portrait.path, @portrait.path, @portrait.path]
convert << tiff.path
end
Expand Down Expand Up @@ -168,7 +168,7 @@
end

it "accepts magick object as source" do
magick = MiniMagick::Tool::Convert.new
magick = ImageProcessing::MiniMagick.convert_shim
magick << fixture_image("rotated.jpg").path
result = ImageProcessing::MiniMagick.source(magick).call
assert_dimensions [600, 800], result
Expand Down Expand Up @@ -568,7 +568,7 @@
it "appends CLI arguments" do
actual = ImageProcessing::MiniMagick.append("-resize", "400x400").call(@portrait)
expected = Tempfile.new(["result", ".jpg"], binmode: true).tap do |tempfile|
MiniMagick::Tool::Convert.new do |cmd|
ImageProcessing::MiniMagick.convert_shim do |cmd|
cmd << @portrait.path
cmd.resize("400x400")
cmd << tempfile.path
Expand Down

0 comments on commit 129b949

Please sign in to comment.