Skip to content

Commit

Permalink
Do not convert JPEG images into JPG (#1904)
Browse files Browse the repository at this point in the history
Images in JPEG format do not need to be converted into JPGs
  • Loading branch information
tvdeyen authored Jul 17, 2020
1 parent 3ede2b6 commit e034b8b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
6 changes: 4 additions & 2 deletions app/models/alchemy/picture/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def encoded_image(image, options = {})

encoding_options = []

if target_format =~ /jpe?g/
convert_format = target_format != image_file_format.sub("jpeg", "jpg")

if target_format =~ /jpe?g/ && convert_format
quality = options[:quality] || Config.get(:output_image_jpg_quality)
encoding_options << "-quality #{quality}"
end
Expand All @@ -80,7 +82,7 @@ def encoded_image(image, options = {})
encoding_options << "-flatten"
end

convertion_needed = target_format != image_file_format || encoding_options.present?
convertion_needed = convert_format || encoding_options.present?

if has_convertible_format? && convertion_needed
image = image.encode(target_format, encoding_options.join(" "))
Expand Down
Binary file added spec/fixtures/image4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 45 additions & 15 deletions spec/models/alchemy/picture_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def decode_dragon_fly_job(url)

context "when a size is passed in" do
let(:options) do
{size: "120x160"}
{ size: "120x160" }
end

it "resizes the image without upsampling it" do
Expand Down Expand Up @@ -133,7 +133,7 @@ def decode_dragon_fly_job(url)

context "with no height given" do
let(:options) do
{size: "40"}
{ size: "40" }
end

it "resizes the image inferring the height" do
Expand All @@ -144,7 +144,7 @@ def decode_dragon_fly_job(url)

context "with no width given" do
let(:options) do
{size: "x30"}
{ size: "x30" }
end

it "resizes the image inferring the width" do
Expand All @@ -163,7 +163,7 @@ def decode_dragon_fly_job(url)

context "when a different format is requested" do
let(:options) do
{format: "gif"}
{ format: "gif" }
end

it "converts the format" do
Expand All @@ -187,7 +187,7 @@ def decode_dragon_fly_job(url)

context "for an animated gif" do
let(:options) do
{format: "png"}
{ format: "png" }
end

let(:image) do
Expand All @@ -206,7 +206,7 @@ def decode_dragon_fly_job(url)

context "requesting a not allowed format" do
let(:options) do
{format: "zip"}
{ format: "zip" }
end

it "returns nil" do
Expand All @@ -221,22 +221,52 @@ def decode_dragon_fly_job(url)

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

it "sets the default quality" do
job = decode_dragon_fly_job(url)
expect(job[1]).to include("-quality 85")
context "and the image file format is not JPG" do
it "sets the default quality" do
job = decode_dragon_fly_job(url)
expect(job[1]).to include("-quality 85")
end

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

it "sets the quality" do
job = decode_dragon_fly_job(url)
expect(job[1]).to include("-quality 30")
end
end
end

context "and quality is passed" do
let(:options) do
{format: "jpg", quality: "30"}
context "and image has jpg format" do
let(:image) do
fixture_file_upload(
File.expand_path("../../fixtures/image4.jpg", __dir__),
"image/jpeg",
)
end

it "does not convert the picture format" do
job = decode_dragon_fly_job(url)
expect(job[1]).to be_nil
end
end

context "and image has jpeg format" do
let(:image) do
fixture_file_upload(
File.expand_path("../../fixtures/image3.jpeg", __dir__),
"image/jpeg",
)
end

it "sets the quality" do
it "does not convert the picture format" do
job = decode_dragon_fly_job(url)
expect(job[1]).to include("-quality 30")
expect(job[1]).to be_nil
end
end
end
Expand Down

0 comments on commit e034b8b

Please sign in to comment.