mirror of
https://github.com/neocities/neocities.git
synced 2025-08-04 16:51:36 +02:00
switch to webp, use png for screenshot returns and be done with the accursed rmagick forever
This commit is contained in:
parent
3eb858bb6f
commit
520413140d
8 changed files with 40 additions and 69 deletions
|
@ -1,8 +1,6 @@
|
|||
require 'rmagick'
|
||||
|
||||
class ThumbnailWorker
|
||||
THUMBNAILS_PATH = Site::THUMBNAILS_ROOT
|
||||
MAXIMUM_IMAGE_SIZE = 2_000_000 # 2MB
|
||||
MAXIMUM_IMAGE_SIZE = 10_000_000 # 10MB
|
||||
|
||||
include Sidekiq::Worker
|
||||
sidekiq_options queue: :thumbnails, retry: 3, backtrace: true
|
||||
|
@ -14,43 +12,33 @@ class ThumbnailWorker
|
|||
site_file_path = site.files_path(path)
|
||||
return unless File.exist?(site_file_path)
|
||||
|
||||
# Large images jam up ImageMagick and eat a ton of memory, so we skip for now.
|
||||
# Large images can eat a ton of memory, so we skip for now.
|
||||
return if File.size(site_file_path) > MAXIMUM_IMAGE_SIZE
|
||||
|
||||
img_list = Magick::ImageList.new
|
||||
|
||||
begin
|
||||
img_list.from_blob File.read(site_file_path)
|
||||
rescue Errno::ENOENT => e # Not found, skip
|
||||
return
|
||||
rescue Magick::ImageMagickError => e
|
||||
GC.start full_mark: true, immediate_sweep: true
|
||||
puts "thumbnail fail: #{site_file_path} #{e.inspect}"
|
||||
return
|
||||
end
|
||||
|
||||
img = img_list.first
|
||||
|
||||
user_thumbnails_path = site.base_thumbnails_path
|
||||
FileUtils.mkdir_p user_thumbnails_path
|
||||
FileUtils.mkdir_p File.join(user_thumbnails_path, File.dirname(path))
|
||||
|
||||
Site::THUMBNAIL_RESOLUTIONS.each do |res|
|
||||
resimg = img.resize_to_fit(*res.split('x').collect {|r| r.to_i})
|
||||
width, height = res.split('x').collect {|r| r.to_i}
|
||||
format = File.extname(path).gsub('.', '')
|
||||
full_thumbnail_path = File.join(user_thumbnails_path, "#{path}.#{res}.webp")
|
||||
|
||||
save_ext = format.match(Site::LOSSY_IMAGE_REGEX) ? 'jpg' : 'png'
|
||||
image = Rszr::Image.load site_file_path
|
||||
if image.width > image.height
|
||||
image.resize! width, :auto
|
||||
else
|
||||
image.resize! :auto, height
|
||||
end
|
||||
|
||||
full_thumbnail_path = File.join(user_thumbnails_path, "#{path}.#{res}.#{save_ext}")
|
||||
begin
|
||||
tmpfile = "/tmp/#{SecureRandom.uuid}.png"
|
||||
image.save(tmpfile)
|
||||
|
||||
resimg.write(full_thumbnail_path) { |i|
|
||||
i.quality = 75
|
||||
}
|
||||
resimg.destroy!
|
||||
#$image_optim.optimize_image! full_thumbnail_path
|
||||
WebP.encode tmpfile, full_thumbnail_path, quality: 70
|
||||
ensure
|
||||
FileUtils.rm tmpfile
|
||||
end
|
||||
end
|
||||
|
||||
img.destroy!
|
||||
GC.start full_mark: true, immediate_sweep: true
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue