mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
switch to faster screenshot resizer lib
This commit is contained in:
parent
d9d129dcc5
commit
5fb1af2a8c
3 changed files with 29 additions and 34 deletions
1
Gemfile
1
Gemfile
|
@ -56,6 +56,7 @@ gem 'maxmind-db'
|
|||
gem 'json', '>= 2.3.0'
|
||||
gem 'nokogiri'
|
||||
gem 'rss'
|
||||
gem 'rszr'
|
||||
|
||||
group :development, :test do
|
||||
gem 'pry'
|
||||
|
|
|
@ -228,6 +228,7 @@ GEM
|
|||
rmagick (5.0.0)
|
||||
rss (0.2.9)
|
||||
rexml
|
||||
rszr (1.3.0)
|
||||
ruby-progressbar (1.11.0)
|
||||
ruby2_keywords (0.0.5)
|
||||
sanitize (6.0.0)
|
||||
|
@ -362,6 +363,7 @@ DEPENDENCIES
|
|||
rinku
|
||||
rmagick
|
||||
rss
|
||||
rszr
|
||||
sanitize
|
||||
sass
|
||||
sequel
|
||||
|
|
|
@ -43,49 +43,41 @@ class ScreenshotWorker
|
|||
wait_time: PAGE_WAIT_TIME
|
||||
)
|
||||
|
||||
img_list = Magick::ImageList.new
|
||||
begin
|
||||
img_list.from_blob HTTP.basic_auth(user: api_user, pass: api_password).get(uri).to_s
|
||||
rescue Magick::ImageMagickError
|
||||
return false
|
||||
end
|
||||
base_image_tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg"
|
||||
File.write base_image_tmpfile_path, HTTP.basic_auth(user: api_user, pass: api_password).get(uri).to_s
|
||||
image = Rszr::Image.load base_image_tmpfile_path
|
||||
|
||||
img_list.new_image(img_list.first.columns, img_list.first.rows) {|i| i.background_color = "white" }
|
||||
img = img_list.reverse.flatten_images
|
||||
img_list.destroy!
|
||||
user_screenshots_path = File.join SCREENSHOTS_PATH, Site.sharding_dir(username), username
|
||||
screenshot_path = File.join user_screenshots_path, File.dirname(path)
|
||||
|
||||
user_screenshots_path = File.join SCREENSHOTS_PATH, Site.sharding_dir(username), username
|
||||
screenshot_path = File.join user_screenshots_path, File.dirname(path)
|
||||
FileUtils.mkdir_p screenshot_path unless Dir.exist?(screenshot_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|
|
||||
width, height = res.split('x').collect {|r| r.to_i}
|
||||
if width == height
|
||||
new_img = image.resize(width, height, crop: :n)
|
||||
else
|
||||
new_img = image.resize width, height
|
||||
end
|
||||
|
||||
if width == height
|
||||
new_img = img.crop_resized width, height, Magick::NorthGravity
|
||||
else
|
||||
new_img = img.scale width, height
|
||||
full_screenshot_path = File.join(user_screenshots_path, "#{path}.#{res}.jpg")
|
||||
tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg"
|
||||
|
||||
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
|
||||
|
||||
full_screenshot_path = File.join(user_screenshots_path, "#{path}.#{res}.jpg")
|
||||
tmpfile_path = "/tmp/#{SecureRandom.uuid}.jpg"
|
||||
|
||||
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
|
||||
true
|
||||
ensure
|
||||
FileUtils.rm base_image_tmpfile_path
|
||||
end
|
||||
|
||||
img.destroy!
|
||||
|
||||
GC.start full_mark: true, immediate_sweep: true
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
sidekiq_retries_exhausted do |msg|
|
||||
|
|
Loading…
Add table
Reference in a new issue