Skip to content

Commit

Permalink
Merge pull request #2692 from tvdeyen/admin-thumbnail-quality
Browse files Browse the repository at this point in the history
Set admin picture thumbnail quality to 90
  • Loading branch information
tvdeyen authored Jan 16, 2024
2 parents 822ea5b + bf6263a commit 1d81e7f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
19 changes: 19 additions & 0 deletions app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

module Alchemy
class Picture < BaseRecord
THUMBNAIL_QUALITY = 90

THUMBNAIL_SIZES = {
small: "80x60",
medium: "160x120",
Expand Down Expand Up @@ -192,6 +194,23 @@ def url(options = {})
nil
end

# Returns an url for the thumbnail representation of the picture
#
# @param [String] size - The size of the thumbnail
# @param [Integer] quality - The quality of the thumbnail
#
# @return [String]
def thumbnail_url(size: "160x120", quality: THUMBNAIL_QUALITY)
return if image_file.nil?

url(
flatten: true,
format: "webp",
quality: quality,
size: size
)
end

# Updates name and tag_list attributes.
#
# Used by +Admin::PicturesController#update_multiple+
Expand Down
3 changes: 2 additions & 1 deletion app/models/concerns/alchemy/picture_thumbnails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def thumbnail_url_options
crop_from: crop && crop_from.presence || default_crop_from&.join("x"),
crop_size: crop && crop_size.presence || default_crop_size&.join("x"),
flatten: true,
format: "webp"
format: "webp",
quality: Alchemy::Picture::THUMBNAIL_QUALITY
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/crop.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= simple_format Alchemy.t(:explain_cropping) %>
<% end %>
<div class="thumbnail_background">
<%= image_tag @picture.url(size: '800x600', flatten: true, format: "webp"), id: 'imageToCrop' %>
<%= image_tag @picture.thumbnail_url(size: '800x600'), id: 'imageToCrop' %>
</div>
<form>
<%= button_tag Alchemy.t(:apply), type: 'submit' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/pictures/_picture.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</sl-tooltip>
</span>
<% end %>
<% picture_url = picture.url(size: preview_size(@size), flatten: true, format: "webp") %>
<% picture_url = picture.thumbnail_url(size: preview_size(@size)) %>
<% image = image_tag(picture_url || "alchemy/missing-image.svg", alt: picture.name) %>
<% if can?(:edit, picture) && picture_url %>
<%= link_to(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<sl-tooltip content="<%= Alchemy.t(:assign_image) %>">
<%= link_to(
image_tag(
picture_to_assign.url(size: preview_size(size), flatten: true, format: "webp") || "alchemy/missing-image.svg",
picture_to_assign.thumbnail_url(size: preview_size(size)) || "alchemy/missing-image.svg",
alt: picture_to_assign.name
),
alchemy.assign_admin_picture_path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
crop_size: nil,
flatten: true,
format: "webp",
quality: Alchemy::Picture::THUMBNAIL_QUALITY,
size: "160x120"
)
end
Expand All @@ -401,6 +402,7 @@
crop_size: nil,
flatten: true,
format: "webp",
quality: Alchemy::Picture::THUMBNAIL_QUALITY,
size: "160x120"
)
end
Expand Down
61 changes: 61 additions & 0 deletions spec/models/alchemy/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,67 @@ module Alchemy
end
end

describe "#thumbnail_url" do
subject(:thumbnail_url) { picture.thumbnail_url }

let(:picture) do
build(:alchemy_picture, image_file: image)
end

context "with no image file present" do
let(:image) { nil }

it { is_expected.to be_nil }
end

context "with image file present" do
let(:image) do
fixture_file_upload(
File.expand_path("../../fixtures/500x500.png", __dir__),
"image/png"
)
end

it "returns the url to the thumbnail" do
expect(picture).to receive(:url).with(
flatten: true,
format: "webp",
quality: Alchemy::Picture::THUMBNAIL_QUALITY,
size: "160x120"
)
thumbnail_url
end

context "with size given" do
subject(:thumbnail_url) { picture.thumbnail_url(size: "800x600") }

it "returns the url to the thumbnail" do
expect(picture).to receive(:url).with(
flatten: true,
format: "webp",
quality: Alchemy::Picture::THUMBNAIL_QUALITY,
size: "800x600"
)
thumbnail_url
end
end

context "with quality given" do
subject(:thumbnail_url) { picture.thumbnail_url(quality: 50) }

it "returns the url to the thumbnail" do
expect(picture).to receive(:url).with(
flatten: true,
format: "webp",
quality: 50,
size: "160x120"
)
thumbnail_url
end
end
end
end

describe "#urlname" do
subject { picture.urlname }

Expand Down

0 comments on commit 1d81e7f

Please sign in to comment.