Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jpeg quality option for jpeg files #2016

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/alchemy/picture_variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def encoded_image(image, options = {})

convert_format = render_format.sub("jpeg", "jpg") != picture.image_file_format.sub("jpeg", "jpg")

if render_format =~ /jpe?g/ && convert_format
if render_format =~ /jpe?g/ && (convert_format || options[:quality])
quality = options[:quality] || Config.get(:output_image_jpg_quality)
encoding_options << "-quality #{quality}"
end
Expand Down
106 changes: 52 additions & 54 deletions spec/models/alchemy/picture_variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,76 +189,74 @@
end
end

context "when jpg format is requested" do
let(:options) do
{ format: "jpg" }
end

context "and the image file format is not JPG" do
it "sets the default quality" do
step = subject.steps[0]
expect(step.name).to eq(:encode)
expect(step.arguments).to eq(["jpg", "-quality 85"])
%w[jpg jpeg].each do |format|
context "when #{format} format is requested" do
let(:options) do
{ format: format }
end

context "and quality is passed" do
let(:options) do
{ format: "jpg", quality: "30" }
end

it "sets the quality" do
context "and the image file format is not JPG" do
it "sets the default quality" do
step = subject.steps[0]
expect(step.name).to eq(:encode)
expect(step.arguments).to eq(["jpg", "-quality 30"])
expect(step.arguments).to eq([format, "-quality 85"])
end
end
end

context "and image has jpg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpg")
end
context "and quality is passed" do
let(:options) do
{ format: format, quality: "30" }
end

it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
it "sets the quality" do
step = subject.steps[0]
expect(step.name).to eq(:encode)
expect(step.arguments).to eq([format, "-quality 30"])
end
end
end
end

context "and image has jpeg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpeg")
end
context "and image has jpg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpg")
end

it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
end
end
end
it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
end

context "when jpeg format is requested" do
let(:options) do
{
format: "jpeg",
}
end
context "and quality is passed in options" do
let(:options) do
{ format: format, quality: "30" }
end

context "and image has jpg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpg")
it "sets the quality" do
step = subject.steps[0]
expect(step.name).to eq(:encode)
expect(step.arguments).to eq([format, "-quality 30"])
end
end
end

it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
end
end
context "and image has jpeg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpeg")
end

context "and image has jpeg format" do
let(:alchemy_picture) do
build_stubbed(:alchemy_picture, image_file: image_file, image_file_format: "jpeg")
end
it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
end

it "does not convert the picture format" do
expect(subject).to_not respond_to(:steps)
context "and quality is passed in options" do
let(:options) do
{ format: format, quality: "30" }
end

it "sets the quality" do
step = subject.steps[0]
expect(step.name).to eq(:encode)
expect(step.arguments).to eq([format, "-quality 30"])
end
end
end
end
end
Expand Down