From 90f64eb407ea5b998b0d0d1e4367f6e919eb0def Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 10 Dec 2019 11:42:13 -0600 Subject: [PATCH] image resizer: remove shell fallback for old versions of libvips. Remove fallback for old versions of Debian. Debian stable now provides libvips 8.7. --- app/logical/danbooru_image_resizer.rb | 69 ++------------------------- 1 file changed, 5 insertions(+), 64 deletions(-) diff --git a/app/logical/danbooru_image_resizer.rb b/app/logical/danbooru_image_resizer.rb index ecb4544a7..82f2581f5 100644 --- a/app/logical/danbooru_image_resizer.rb +++ b/app/logical/danbooru_image_resizer.rb @@ -3,29 +3,17 @@ module DanbooruImageResizer # Taken from ArgyllCMS 2.0.0 (see also: https://ninedegreesbelow.com/photography/srgb-profile-comparison.html) SRGB_PROFILE = "#{Rails.root}/config/sRGB.icm" + # http://jcupitt.github.io/libvips/API/current/libvips-resample.html#vips-thumbnail THUMBNAIL_OPTIONS = { size: :down, linear: false, auto_rotate: false, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE } - # http://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-jpegsave - JPEG_OPTIONS = { background: 255, strip: true, interlace: true, optimize_coding: true } CROP_OPTIONS = { linear: false, auto_rotate: false, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE, crop: :attention } - # XXX libvips-8.4 on Debian doesn't support the `Vips::Image.thumbnail` method. - # On 8.4 we have to shell out to vipsthumbnail instead. Remove when Debian supports 8.5. - def resize(file, width, height, quality = 90) - if Vips.at_least_libvips?(8, 5) - resize_ruby(file, width, height, quality) - else - resize_shell(file, width, height, quality) - end - end - - def crop(file, width, height, quality = 90) - crop_shell(file, width, height, quality) - end + # http://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-jpegsave + JPEG_OPTIONS = { background: 255, strip: true, interlace: true, optimize_coding: true } # https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking # http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html - def resize_ruby(file, width, height, resize_quality) + def resize(file, width, height, resize_quality = 90) output_file = Tempfile.new resized_image = Vips::Image.thumbnail(file.path, width, height: height, **THUMBNAIL_OPTIONS) resized_image.jpegsave(output_file.path, Q: resize_quality, **JPEG_OPTIONS) @@ -33,7 +21,7 @@ module DanbooruImageResizer output_file end - def crop_ruby(file, width, height, resize_quality) + def crop(file, width, height, resize_quality = 90) return nil unless Danbooru.config.enable_image_cropping output_file = Tempfile.new @@ -43,53 +31,6 @@ module DanbooruImageResizer output_file end - def resize_shell(file, width, height, quality) - output_file = Tempfile.new(["resize", ".jpg"]) - - # --size=WxH will upscale if the image is smaller than the target size. - # Fix the target size so that it's not bigger than the image. - image = Vips::Image.new_from_file(file.path) - target_width = [image.width, width].min - target_height = [image.height, height].min - - arguments = [ - file.path, - "--eprofile=#{SRGB_PROFILE}", - "--iprofile=#{SRGB_PROFILE}", - "--size=#{target_width}x#{target_height}", - "--format=#{output_file.path}[Q=#{quality},background=255,strip,interlace,optimize_coding]" - ] - - success = system("vipsthumbnail", *arguments) - raise RuntimeError, "vipsthumbnail failed (exit status: #{$?.exitstatus})" if !success - - output_file - end - - def crop_shell(file, width, height, quality) - return nil unless Danbooru.config.enable_image_cropping - - output_file = Tempfile.new(["crop", ".jpg"]) - - # --size=WxH will upscale if the image is smaller than the target size. - # Fix the target size so that it's not bigger than the image. - image = Vips::Image.new_from_file(file.path) - - arguments = [ - file.path, - "--eprofile=#{SRGB_PROFILE}", - "--iprofile=#{SRGB_PROFILE}", - "--smartcrop=attention", - "--size=#{width}x#{height}", - "--format=#{output_file.path}[Q=#{quality},background=255,strip,interlace,optimize_coding]" - ] - - success = system("vipsthumbnail", *arguments) - raise RuntimeError, "vipsthumbnail failed (exit status: #{$?.exitstatus})" if !success - - output_file - end - def validate_shell(file) temp = Tempfile.new("validate") output, status = Open3.capture2e("vips stats #{file.path} #{temp.path}.v")