-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The build in thumb processor does not support to resize the image after it has been cropped. In order to make it work in dragonfly 1.4 we add our own processor.
- Loading branch information
Showing
7 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dragonfly/image_magick/commands" | ||
|
||
module Alchemy | ||
module Dragonfly | ||
module Processors | ||
class CropResize | ||
include ::Dragonfly::ParamValidators | ||
|
||
IS_CROP_ARGUMENT = ->(args_string) { | ||
args_string.match?(::Dragonfly::ImageMagick::Processors::Thumb::CROP_GEOMETRY) | ||
} | ||
|
||
IS_RESIZE_ARGUMENT = ->(args_string) { | ||
args_string.match?(::Dragonfly::ImageMagick::Processors::Thumb::RESIZE_GEOMETRY) | ||
} | ||
|
||
def call(content, crop_argument, resize_argument) | ||
validate!(crop_argument, &IS_CROP_ARGUMENT) | ||
validate!(resize_argument, &IS_RESIZE_ARGUMENT) | ||
::Dragonfly::ImageMagick::Commands.convert( | ||
content, | ||
"-crop #{crop_argument} -resize #{resize_argument}" | ||
) | ||
end | ||
|
||
def update_url(attrs, _args = "", opts = {}) | ||
format = opts["format"] | ||
attrs.ext = format if format | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
require_relative "../../../support/dragonfly_test_app" | ||
|
||
RSpec.describe Alchemy::Dragonfly::Processors::CropResize do | ||
let(:app) { dragonfly_test_app } | ||
let(:file) { Pathname.new(File.expand_path("../../../fixtures/80x60.png", __dir__)) } | ||
let(:image) { Dragonfly::Content.new(app, file) } | ||
let(:processor) { described_class.new } | ||
|
||
it "validates bad crop and resize arguments" do | ||
expect { | ||
processor.call(image, "h4ck", "m3") | ||
}.to raise_error(Dragonfly::ParamValidators::InvalidParameter) | ||
end | ||
|
||
it "works with correct crop and resize arguments" do | ||
expect { | ||
processor.call(image, "4x4+0+0", "20x20>") | ||
}.to_not raise_error | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
def dragonfly_test_app(name = nil) | ||
app = Dragonfly::App.instance(name) | ||
app.datastore = Dragonfly::MemoryDataStore.new | ||
app.secret = "test secret" | ||
app | ||
end |