account_header.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. module AccountHeader
  3. extend ActiveSupport::Concern
  4. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  5. LIMIT = 2.megabytes
  6. MAX_PIXELS = 750_000 # 1500x500px
  7. class_methods do
  8. def header_styles(file)
  9. styles = { original: { pixels: MAX_PIXELS, file_geometry_parser: FastGeometryParser } }
  10. styles[:static] = { format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif'
  11. styles
  12. end
  13. private :header_styles
  14. end
  15. included do
  16. # Header upload
  17. has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail]
  18. validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
  19. validates_attachment_size :header, less_than: LIMIT
  20. remotable_attachment :header, LIMIT
  21. end
  22. def header_original_url
  23. header.url(:original)
  24. end
  25. def header_static_url
  26. header_content_type == 'image/gif' ? header.url(:static) : header_original_url
  27. end
  28. end