Skip to content

Commit

Permalink
Add some optimzation to avatar and header image variants
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Nov 6, 2023
1 parent 76dcd8c commit 2c64f95
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class User < ActiveRecord::Base
AVATAR_IMAGE_VARIANTS = {
small: { resize_to_fill: [100, 100] },
medium: { resize_to_fill: [256, 256] },
large: { resize_to_fill: [512, 512] },
small: { resize_to_fill: [100, 100], saver: { quality: 85 }, strip: true },
medium: { resize_to_fill: [256, 256], saver: { quality: 85 }, strip: true },
large: { resize_to_fill: [512, 512], saver: { quality: 85 }, strip: true },
}.freeze

HEADER_IMAGE_VARIANTS = {
small: { resize_to_fill: [800, 267], gravity: 'center' },
medium: { resize_to_fill: [1500, 500], gravity: 'center' },
large: { resize_to_fill: [3000, 1000], gravity: 'center' },
small: { resize_to_fill: [800, 267], gravity: 'center', saver: { quality: 85 }, strip: true },
medium: { resize_to_fill: [1500, 500], gravity: 'center', saver: { quality: 85 }, strip: true },
large: { resize_to_fill: [3000, 1000], gravity: 'center', saver: { quality: 85 }, strip: true },
}.freeze

self.ignored_columns = ["ga_view_id", "ga_property_id"].freeze
Expand Down Expand Up @@ -76,15 +76,19 @@ def gravatar_url(size = 256)

def avatar_url(size = :medium)
url = if avatar_image.attached?
Rails.application.routes.url_helpers.cdn_blob_url(avatar_image.variant(**AVATAR_IMAGE_VARIANTS[size]))
Rails.application.routes.url_helpers.cdn_blob_url(
avatar_image.variant(**AVATAR_IMAGE_VARIANTS[size])
)
end

url || gravatar_url(AVATAR_IMAGE_VARIANTS.dig(size, :resize_to_fill)&.first)
end

def header_image_url(size = :medium)
if header_image.attached?
Rails.application.routes.url_helpers.cdn_blob_url(header_image.variant(**HEADER_IMAGE_VARIANTS[size]))
Rails.application.routes.url_helpers.cdn_blob_url(
header_image.variant(**HEADER_IMAGE_VARIANTS[size])
)
end
end

Expand Down

0 comments on commit 2c64f95

Please sign in to comment.