You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
822 B

  1. # frozen_string_literal: true
  2. module Paperclip
  3. class LazyThumbnail < Paperclip::Thumbnail
  4. def make
  5. return File.open(@file.path) unless needs_convert?
  6. min_side = [@current_geometry.width, @current_geometry.height].min
  7. options[:geometry] = "#{min_side.to_i}x#{min_side.to_i}#" if @target_geometry.square? && min_side < @target_geometry.width
  8. Paperclip::Thumbnail.make(file, options, attachment)
  9. end
  10. private
  11. def needs_convert?
  12. needs_different_geometry? || needs_different_format?
  13. end
  14. def needs_different_geometry?
  15. !@target_geometry.nil? && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height
  16. end
  17. def needs_different_format?
  18. @format.present? && @current_format != @format
  19. end
  20. end
  21. end