1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-13 06:09:08 +09:00
YuruToot/lib/paperclip/lazy_thumbnail.rb
Eugen Rochko 0fb0037ca7
Resize images by area instead of fixed dimensions (#8083)
To improve the way super tall or super ride images are treated, the
numbers remain the same, 1280x1280 and 400x400, but if an image
is less in one dimension than the other, the other can become larger

Thanks to @WAHa_06x36@mastodon.social for the tip
2018-07-28 03:33:00 +02:00

36 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Paperclip
class LazyThumbnail < Paperclip::Thumbnail
def make
return File.open(@file.path) unless needs_convert?
if options[:geometry]
min_side = [@current_geometry.width, @current_geometry.height].min.to_i
options[:geometry] = "#{min_side}x#{min_side}#" if @target_geometry.square? && min_side < @target_geometry.width
elsif options[:pixels]
width = Math.sqrt(options[:pixels] * (@current_geometry.width.to_f / @current_geometry.height.to_f)).round.to_i
height = Math.sqrt(options[:pixels] * (@current_geometry.height.to_f / @current_geometry.width.to_f)).round.to_i
options[:geometry] = "#{width}x#{height}>"
end
Paperclip::Thumbnail.make(file, options, attachment)
end
private
def needs_convert?
needs_different_geometry? || needs_different_format?
end
def needs_different_geometry?
(options[:geometry] && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height) ||
(options[:pixels] && @current_geometry.width * @current_geometry.height > options[:pixels])
end
def needs_different_format?
@format.present? && @current_format != @format
end
end
end