From 0fb0037ca7ea9910b490818a1cc13f4005ba6134 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 28 Jul 2018 03:33:00 +0200 Subject: 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 --- lib/paperclip/lazy_thumbnail.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/paperclip') diff --git a/lib/paperclip/lazy_thumbnail.rb b/lib/paperclip/lazy_thumbnail.rb index aafa21343..ea675a5bf 100644 --- a/lib/paperclip/lazy_thumbnail.rb +++ b/lib/paperclip/lazy_thumbnail.rb @@ -5,8 +5,14 @@ module Paperclip def make return File.open(@file.path) unless needs_convert? - 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 + 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 @@ -18,7 +24,8 @@ module Paperclip end def needs_different_geometry? - !@target_geometry.nil? && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height + (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? -- cgit