Skip to content

Commit

Permalink
fix: simplify resize_and_pad and support gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 27, 2023
1 parent d66de95 commit 326bd49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 100 deletions.
36 changes: 16 additions & 20 deletions lib/imgproxy-rails/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ class Transformer
modulate: proc { |p| modulate(p) }
}.freeze

GRAVITY = {
"north" => "no",
"north-east" => "ne",
"east" => "ea",
"south-east" => "se",
"south" => "so",
"south-west" => "sw",
"west" => "we",
"north-west" => "nw",
"centre" => "ce"
}.freeze

PASSTHROUGH_OPTIONS = Set.new([
"rotate",
"sharpen",
Expand Down Expand Up @@ -49,31 +61,15 @@ def resize_and_pad(p, m)
options ||= {}

result = {width: target_width, height: target_height, extend: true}
return result unless m["width"] && m["height"]

aspect_ratio = m["width"].to_f / m["height"]
if aspect_ratio > 1
# add vertical padding
final_height = target_width.to_f / aspect_ratio
padding_length = ((target_height - final_height) / 2).round
result[:padding] = [padding_length, 0]

# setting min-width for correct upscaling
result[:mw] = target_width
elsif aspect_ratio < 1
# add horizontal padding
final_width = target_height.to_f * aspect_ratio
padding_length = ((target_width - final_width) / 2).round
result[:padding] = [0, padding_length]

# setting min-height for correct upscaling
result[:mh] = target_height
end

if (background = convert_color(options[:background]))
result[:background] = background
end

if (gravity = GRAVITY[options[:gravity].to_s])
result[:extend] = {extend: true, gravity: gravity}
end

result
end

Expand Down
97 changes: 17 additions & 80 deletions spec/lib/imgproxy-rails/transformer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,98 +60,35 @@
it { is_expected.to eq(expected) }
end

context "when landscape" do
let(:params) { [1000, 1000] }
let(:meta) { {"width" => 1664, "height" => 960} }
let(:params) { [1000, 1000] }
let(:meta) { {"width" => 1664, "height" => 960} }

it_behaves_like "transforms to",
specify do
is_expected.to eq(
width: 1000,
height: 1000,
extend: true,
padding: [212, 0],
mw: 1000

context "when target height and width > original image" do
let(:params) { [2000, 2000] }
let(:meta) { {"width" => 1664, "height" => 960} }

it_behaves_like "transforms to",
width: 2000,
height: 2000,
extend: true,
padding: [423, 0],
mw: 2000
end

context "when target height and width < original image" do
let(:params) { [500, 500] }
let(:meta) { {"width" => 1664, "height" => 960} }

it_behaves_like "transforms to",
width: 500,
height: 500,
extend: true,
padding: [106, 0],
mw: 500
end
extend: true
)
end

context "when vertical" do
let(:params) { [1000, 1000] }
let(:meta) { {"width" => 799, "height" => 1280} }
context "when background is present" do
let(:params) { [1000, 1000, {background: "#bbbbc4"}] }
let(:meta) { {"width" => 100, "height" => 100} }

it_behaves_like "transforms to",
width: 1000,
height: 1000,
extend: true,
padding: [0, 188],
mh: 1000
it { is_expected.to include(background: "bbbbc4") }

context "when target height and width > original image" do
let(:params) { [2000, 2000] }
let(:meta) { {"width" => 799, "height" => 1280} }
context "when invalid" do
let(:params) { [1000, 1000, {background: "invalid"}] }

it_behaves_like "transforms to",
width: 2000,
height: 2000,
extend: true,
padding: [0, 376],
mh: 2000
end

context "when target height and width < original image" do
let(:params) { [500, 500] }
let(:meta) { {"width" => 799, "height" => 1280} }

it_behaves_like "transforms to",
width: 500,
height: 500,
extend: true,
padding: [0, 94],
mh: 500
it { is_expected.to_not have_key(:background) }
end
end

context "when square" do
let(:params) { [1000, 1000] }
let(:meta) { {"width" => 500, "height" => 500} }
context "when gravity is present" do
let(:params) { [1000, 1000, {gravity: :"north-east"}] }
let(:meta) { {"width" => 100, "height" => 100} }

it_behaves_like "transforms to", width: 1000, height: 1000, extend: true
end

describe "background option" do
context "when background is not present" do
let(:params) { [1000, 1000, {background: "#bbbbc4"}] }
let(:meta) { {"width" => 100, "height" => 100} }

it { is_expected.to include(background: "bbbbc4") }

context "when invalid" do
let(:params) { [1000, 1000, {background: "invalid"}] }

it { is_expected.to_not have_key(:background) }
end
end
it { expect(subject.fetch(:extend)).to eq(extend: true, gravity: "ne") }
end
end
end

0 comments on commit 326bd49

Please sign in to comment.