forked from e621ng/e621ng
[Posts] Generate samples with background color (#649)
This commit is contained in:
parent
4f61a94504
commit
0df2e77ab5
@ -6,23 +6,29 @@ module DanbooruImageResizer
|
|||||||
# https://www.libvips.org/API/current/libvips-resample.html#vips-thumbnail
|
# https://www.libvips.org/API/current/libvips-resample.html#vips-thumbnail
|
||||||
THUMBNAIL_OPTIONS = { size: :down, linear: false, no_rotate: true, export_profile: "srgb", import_profile: "srgb" }.freeze
|
THUMBNAIL_OPTIONS = { size: :down, linear: false, no_rotate: true, export_profile: "srgb", import_profile: "srgb" }.freeze
|
||||||
# https://www.libvips.org/API/current/VipsForeignSave.html#vips-jpegsave
|
# https://www.libvips.org/API/current/VipsForeignSave.html#vips-jpegsave
|
||||||
JPEG_OPTIONS = { background: 0, strip: true, interlace: true, optimize_coding: true }.freeze
|
JPEG_OPTIONS = { strip: true, interlace: true, optimize_coding: true }.freeze
|
||||||
CROP_OPTIONS = { linear: false, no_rotate: true, export_profile: "srgb", import_profile: "srgb", crop: :attention }.freeze
|
CROP_OPTIONS = { linear: false, no_rotate: true, export_profile: "srgb", import_profile: "srgb", crop: :attention }.freeze
|
||||||
|
|
||||||
def resize(file, width, height, resize_quality = 90)
|
def resize(file, width, height, resize_quality = 90, background_color: "000000")
|
||||||
|
r = background_color[0..1].to_i(16)
|
||||||
|
g = background_color[2..3].to_i(16)
|
||||||
|
b = background_color[4..5].to_i(16)
|
||||||
output_file = Tempfile.new
|
output_file = Tempfile.new
|
||||||
resized_image = thumbnail(file, width, height, THUMBNAIL_OPTIONS)
|
resized_image = thumbnail(file, width, height, THUMBNAIL_OPTIONS)
|
||||||
resized_image.jpegsave(output_file.path, Q: resize_quality, **JPEG_OPTIONS)
|
resized_image.jpegsave(output_file.path, Q: resize_quality, background: [r, g, b], **JPEG_OPTIONS)
|
||||||
|
|
||||||
output_file
|
output_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def crop(file, width, height, resize_quality = 90)
|
def crop(file, width, height, resize_quality = 90, background_color: "000000")
|
||||||
return nil unless Danbooru.config.enable_image_cropping?
|
return nil unless Danbooru.config.enable_image_cropping?
|
||||||
|
|
||||||
|
r = background_color[0..1].to_i(16)
|
||||||
|
g = background_color[2..3].to_i(16)
|
||||||
|
b = background_color[4..5].to_i(16)
|
||||||
output_file = Tempfile.new
|
output_file = Tempfile.new
|
||||||
resized_image = thumbnail(file, width, height, CROP_OPTIONS)
|
resized_image = thumbnail(file, width, height, CROP_OPTIONS)
|
||||||
resized_image.jpegsave(output_file.path, Q: resize_quality, **JPEG_OPTIONS)
|
resized_image.jpegsave(output_file.path, Q: resize_quality, background: [r, g, b], **JPEG_OPTIONS)
|
||||||
|
|
||||||
output_file
|
output_file
|
||||||
end
|
end
|
||||||
|
@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
module PostThumbnailer
|
module PostThumbnailer
|
||||||
extend self
|
extend self
|
||||||
def generate_resizes(file, height, width, type)
|
def generate_resizes(file, height, width, type, background_color: "000000")
|
||||||
if type == :video
|
if type == :video
|
||||||
video = FFMPEG::Movie.new(file.path)
|
video = FFMPEG::Movie.new(file.path)
|
||||||
crop_file = generate_video_crop_for(video, Danbooru.config.small_image_width)
|
crop_file = generate_video_crop_for(video, Danbooru.config.small_image_width)
|
||||||
preview_file = generate_video_preview_for(file.path, Danbooru.config.small_image_width)
|
preview_file = generate_video_preview_for(file.path, Danbooru.config.small_image_width)
|
||||||
sample_file = generate_video_sample_for(file.path)
|
sample_file = generate_video_sample_for(file.path)
|
||||||
elsif type == :image
|
elsif type == :image
|
||||||
preview_file = DanbooruImageResizer.resize(file, Danbooru.config.small_image_width, Danbooru.config.small_image_width, 87)
|
preview_file = DanbooruImageResizer.resize(file, Danbooru.config.small_image_width, Danbooru.config.small_image_width, 87, background_color: background_color)
|
||||||
crop_file = DanbooruImageResizer.crop(file, Danbooru.config.small_image_width, Danbooru.config.small_image_width, 87)
|
crop_file = DanbooruImageResizer.crop(file, Danbooru.config.small_image_width, Danbooru.config.small_image_width, 87, background_color: background_color)
|
||||||
if width > Danbooru.config.large_image_width
|
if width > Danbooru.config.large_image_width
|
||||||
sample_file = DanbooruImageResizer.resize(file, Danbooru.config.large_image_width, height, 87)
|
sample_file = DanbooruImageResizer.resize(file, Danbooru.config.large_image_width, height, 87, background_color: background_color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -234,7 +234,7 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
def regenerate_image_samples!
|
def regenerate_image_samples!
|
||||||
file = self.file()
|
file = self.file()
|
||||||
preview_file, crop_file, sample_file = ::PostThumbnailer.generate_resizes(file, image_height, image_width, is_video? ? :video : :image)
|
preview_file, crop_file, sample_file = ::PostThumbnailer.generate_resizes(file, image_height, image_width, is_video? ? :video : :image, background_color: bg_color)
|
||||||
storage_manager.store_file(sample_file, self, :large) if sample_file.present?
|
storage_manager.store_file(sample_file, self, :large) if sample_file.present?
|
||||||
storage_manager.store_file(preview_file, self, :preview) if preview_file.present?
|
storage_manager.store_file(preview_file, self, :preview) if preview_file.present?
|
||||||
storage_manager.store_file(crop_file, self, :crop) if crop_file.present?
|
storage_manager.store_file(crop_file, self, :crop) if crop_file.present?
|
||||||
|
Loading…
Reference in New Issue
Block a user