Skip to content

Commit

Permalink
Merge pull request #2016 from kulturbande/bugfix/jpeg_quality
Browse files Browse the repository at this point in the history
Fix jpeg quality option for jpeg files
  • Loading branch information
tvdeyen authored Feb 11, 2021
2 parents c295b66 + 8cc2771 commit 9c44beb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
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

0 comments on commit 9c44beb

Please sign in to comment.