diff --git a/app/models/alchemy/picture_variant.rb b/app/models/alchemy/picture_variant.rb index fe55c01c44..681b3b7086 100644 --- a/app/models/alchemy/picture_variant.rb +++ b/app/models/alchemy/picture_variant.rb @@ -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 diff --git a/spec/models/alchemy/picture_variant_spec.rb b/spec/models/alchemy/picture_variant_spec.rb index 1ac41c73a7..123ce9a78d 100644 --- a/spec/models/alchemy/picture_variant_spec.rb +++ b/spec/models/alchemy/picture_variant_spec.rb @@ -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