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

Do not convert JPEG images into JPG #1904

Merged
merged 1 commit into from Jul 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/models/alchemy/picture/url.rb
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
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
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