switch to faster screenshot resizer lib

This commit is contained in:
Kyle Drake 2023-01-17 19:04:06 -06:00
parent d9d129dcc5
commit 5fb1af2a8c
3 changed files with 29 additions and 34 deletions

View file

@ -56,6 +56,7 @@ gem 'maxmind-db'
gem 'json', '>= 2.3.0' gem 'json', '>= 2.3.0'
gem 'nokogiri' gem 'nokogiri'
gem 'rss' gem 'rss'
gem 'rszr'
group :development, :test do group :development, :test do
gem 'pry' gem 'pry'

View file

@ -228,6 +228,7 @@ GEM
rmagick (5.0.0) rmagick (5.0.0)
rss (0.2.9) rss (0.2.9)
rexml rexml
rszr (1.3.0)
ruby-progressbar (1.11.0) ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
sanitize (6.0.0) sanitize (6.0.0)
@ -362,6 +363,7 @@ DEPENDENCIES
rinku rinku
rmagick rmagick
rss rss
rszr
sanitize sanitize
sass sass
sequel sequel

View file

@ -43,49 +43,41 @@ class ScreenshotWorker
wait_time: PAGE_WAIT_TIME wait_time: PAGE_WAIT_TIME
) )
img_list = Magick::ImageList.new
begin begin
img_list.from_blob HTTP.basic_auth(user: api_user, pass: api_password).get(uri).to_s base_image_tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg"
rescue Magick::ImageMagickError File.write base_image_tmpfile_path, HTTP.basic_auth(user: api_user, pass: api_password).get(uri).to_s
return false image = Rszr::Image.load base_image_tmpfile_path
end
img_list.new_image(img_list.first.columns, img_list.first.rows) {|i| i.background_color = "white" } user_screenshots_path = File.join SCREENSHOTS_PATH, Site.sharding_dir(username), username
img = img_list.reverse.flatten_images screenshot_path = File.join user_screenshots_path, File.dirname(path)
img_list.destroy!
user_screenshots_path = File.join SCREENSHOTS_PATH, Site.sharding_dir(username), username FileUtils.mkdir_p screenshot_path unless Dir.exist?(screenshot_path)
screenshot_path = File.join user_screenshots_path, File.dirname(path)
FileUtils.mkdir_p screenshot_path unless Dir.exist?(screenshot_path) Site::SCREENSHOT_RESOLUTIONS.each do |res|
width, height = res.split('x').collect {|r| r.to_i}
Site::SCREENSHOT_RESOLUTIONS.each do |res| if width == height
width, height = res.split('x').collect {|r| r.to_i} new_img = image.resize(width, height, crop: :n)
else
new_img = image.resize width, height
end
if width == height full_screenshot_path = File.join(user_screenshots_path, "#{path}.#{res}.jpg")
new_img = img.crop_resized width, height, Magick::NorthGravity tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg"
else
new_img = img.scale width, height begin
new_img.save tmpfile_path, quality: 92
$image_optim.optimize_image! tmpfile_path
File.open(full_screenshot_path, 'wb') {|file| file.write File.read(tmpfile_path)}
ensure
FileUtils.rm tmpfile_path
end
end end
full_screenshot_path = File.join(user_screenshots_path, "#{path}.#{res}.jpg") true
tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg" ensure
FileUtils.rm base_image_tmpfile_path
begin
new_img.write(tmpfile_path) { |i| i.quality = 92 }
new_img.destroy!
$image_optim.optimize_image! tmpfile_path
File.open(full_screenshot_path, 'wb') {|file| file.write File.read(tmpfile_path)}
ensure
FileUtils.rm tmpfile_path
end
end end
img.destroy!
GC.start full_mark: true, immediate_sweep: true
true
end end
sidekiq_retries_exhausted do |msg| sidekiq_retries_exhausted do |msg|