This repository has been archived on 2024-06-09. You can view files and clone it, but cannot push or open issues/pull-requests.
2018-02-21 03:40:12 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Paperclip
|
|
|
|
class LazyThumbnail < Paperclip::Thumbnail
|
|
|
|
def make
|
2018-02-22 00:28:19 +01:00
|
|
|
return File.open(@file.path) unless needs_convert?
|
2018-03-06 06:29:01 +01:00
|
|
|
|
|
|
|
min_side = [@current_geometry.width, @current_geometry.height].min
|
|
|
|
options[:geometry] = "#{min_side.to_i}x#{min_side.to_i}#" if @target_geometry.square? && min_side < @target_geometry.width
|
|
|
|
|
2018-02-21 03:40:12 +01:00
|
|
|
Paperclip::Thumbnail.make(file, options, attachment)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def needs_convert?
|
|
|
|
needs_different_geometry? || needs_different_format?
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_different_geometry?
|
|
|
|
!@target_geometry.nil? && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_different_format?
|
|
|
|
@format.present? && @current_format != @format
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|