Skip to content

A platform for community discussion. Free, open, simple.

PERF: Process letter avatars and dominant colors with libvips (#42854)

Alan Guo Xiang Tan189478b45ba0
diff --git a/Gemfile b/Gemfile
index 636de710f11..4430a0cf40f 100644
--- a/Gemfile
+++ b/Gemfile
@@ -60,6 +60,8 @@ gem "message_bus"
gem "rails_multisite"
gem "fastimage"
+gem "msgpack"
+gem "ruby-vips", "~> 2.3", require: false
gem "aws-sdk-s3", require: false
gem "aws-sdk-sns", require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index 0067a71ae64..a65d336b5d3 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -316,7 +316,7 @@ GEM
simpleidn (~> 0.2)
jwt (2.10.3)
base64
- landlock (0.4.1)
+ landlock (0.5)
language_server-protocol (3.17.0.6)
libv8-node (24.12.0.1-aarch64-linux)
libv8-node (24.12.0.1-aarch64-linux-musl)
@@ -685,6 +685,9 @@ GEM
ruby-readability (0.7.3)
guess_html_encoding (>= 0.0.4)
nokogiri (>= 1.6.0)
+ ruby-vips (2.3.0)
+ ffi (~> 1.12)
+ logger
ruby2_keywords (0.0.5)
rubyzip (3.5.0)
samovar (2.5.1)
@@ -902,6 +905,7 @@ DEPENDENCIES
mini_suffix
minio_runner
mocha
+ msgpack
multi_json
multipart-post
mustache
@@ -958,6 +962,7 @@ DEPENDENCIES
ruby-prof
ruby-rc4
ruby-readability
+ ruby-vips (~> 2.3)
rubyzip
sanitize
sassc-embedded
@@ -1107,7 +1112,7 @@ CHECKSUMS
json_completer (1.2.0) sha256=4665749172634eccb208c562eb59ea81dd7a612a1fa9a36df441ac473ff177b1
json_schemer (2.5.0) sha256=2f01fb4cce721a4e08dd068fc2030cffd0702a7f333f1ea2be6e8991f00ae396
jwt (2.10.3) sha256=e4d9352fbc7309b1a7448c7dd713dfe4d8c47077af80759cdbed8f878ea0b484
- landlock (0.4.1) sha256=95c9b119ff831fae35756ddcd5e9d5f99b9705be71f37a0972f86c825b78b14f
+ landlock (0.5) sha256=fbe450a0f73be398a7dd75e7b731bcf3764442228c8a259b6fa651affb431d33
language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0
libv8-node (24.12.0.1-aarch64-linux) sha256=22bd0246cde85d70c88cf772727ac3677a20ac1d169105f82efe5f1f7c39f755
libv8-node (24.12.0.1-aarch64-linux-musl) sha256=791b5960f79525e87d1bd1e5f2bf3715ef2a38bac6c97f297853e98a8411ba89
@@ -1266,6 +1271,7 @@ CHECKSUMS
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
ruby-rc4 (0.1.5) sha256=00cc40a39d20b53f5459e7ea006a92cf584e9bc275e2a6f7aa1515510e896c03
ruby-readability (0.7.3) sha256=bd213fab037118cc15d999e167a8ea2cc6b689dc78c033e78fb36a2a37efc241
+ ruby-vips (2.3.0) sha256=e685ec02c13969912debbd98019e50492e12989282da5f37d05f5471442f5374
ruby2_keywords (0.0.5) sha256=ffd13740c573b7301cf7a2e61fc857b2a8e3d3aff32545d6f8300d8bae10e3ef
rubyzip (3.5.0) sha256=f77c49d737a849b662f6ca4de473f31b4ecd8288f1c9f901a5c820641f0f8d7c
samovar (2.5.1) sha256=8a9fc41eb8868084f0321eb41678c485cfbc7d282fb306c0be67c3284b1d2394
diff --git a/app/models/upload.rb b/app/models/upload.rb
index 0ef087df7de..d73814f1fd3 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -378,7 +378,8 @@ class Upload < ActiveRecord::Base
def calculate_dominant_color!(local_path = nil)
color = nil
- color = "" if !FileHelper.is_supported_image?("image.#{extension}") || extension == "svg"
+ color = "" if !FileHelper.is_supported_image?("image.#{extension}") || extension == "svg" ||
+ (GlobalSetting.enable_vips_image_processing && extension == "ico")
if color.nil?
local_path ||=
@@ -396,33 +397,39 @@ class Upload < ActiveRecord::Base
color ||=
begin
- data =
- ImageMagick.magick(
- local_path,
- "-depth",
- "8",
- "-resize",
- "1x1",
- "-define",
- "histogram:unique-colors=true",
- "-format",
- "%c",
- "histogram:info:",
- operation: :upload_dominant_color,
- read: [local_path],
- nice: 10,
- timeout: DOMINANT_COLOR_COMMAND_TIMEOUT_SECONDS,
- )
-
- # Output format:
- # 1: (110.873,116.226,93.8821) #6F745E srgb(43.4798%,45.5789%,36.8165%)
-
- color = data[/#([0-9A-F]{6})/, 1]
-
- raise "Calculated dominant color but unable to parse output:\n#{data}" if color.nil?
-
- color
- rescue Discourse::Utils::CommandError
+ if GlobalSetting.enable_vips_image_processing
+ color =
+ DiscourseVips.dominant_color(
+ input_path: local_path,
+ timeout: DOMINANT_COLOR_COMMAND_TIMEOUT_SECONDS,
+ )
+ color = "" if color !~ /\A[0-9A-F]{6}\z/
+ color
+ else
+ data =
+ ImageMagick.magick(
+ local_path,
+ "-depth",
+ "8",
+ "-resize",
+ "1x1",
+ "-define",
+ "histogram:unique-colors=true",
+ "-format",
+ "%c",
+ "histogram:info:",
+ operation: :upload_dominant_color,
+ read: [local_path],
+ nice: 10,
+ timeout: DOMINANT_COLOR_COMMAND_TIMEOUT_SECONDS,
+ )
+
+ color = data[/#([0-9A-F]{6})/, 1]
+ raise "Calculated dominant color but unable to parse output:\n#{data}" if color.nil?
+
+ color
+ end
+ rescue DiscourseVips::Error, Discourse::Utils::CommandError
# Timeout or unable to parse image
# This can happen due to bad user input - ignore and save
# an empty string to prevent re-evaluation
diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.conf
index 64c654bc626..90ab6daf9c4 100644
--- a/config/discourse_defaults.conf
+++ b/config/discourse_defaults.conf
@@ -412,5 +412,8 @@ asset_url_salt =
# Enable Ruby YJIT to get better performances at the cost of using more memory.
yjit_enabled = false
+# Use libvips for letter avatar generation and dominant color detection.
+enable_vips_image_processing = false
+
# Enable mini_racer single-threaded mode, and boot contexts before forking
mini_racer_single_threaded = true
diff --git a/config/initializers/003-vips.rb b/config/initializers/003-vips.rb
new file mode 100644
index 00000000000..94975aef616
--- /dev/null
+++ b/config/initializers/003-vips.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require "discourse_vips"
+
+if !Rails.env.production?
+ Rails.application.config.after_initialize do
+ next if !GlobalSetting.enable_vips_image_processing
+
+ DiscourseVips.version
+ rescue DiscourseVips::Error => error
+ raise LoadError, <<~MESSAGE, error.backtrace
+ Discourse requires libvips for image processing, but it could not be loaded.
+
+ Install libvips, then restart Discourse:
+ https://github.com/libvips/libvips/wiki#building-and-installing
+ MESSAGE
+ end
+end
diff --git a/config/pitchfork.conf.rb b/config/pitchfork.conf.rb
index 01e5f7faf91..8385216f703 100644
--- a/config/pitchfork.conf.rb
+++ b/config/pitchfork.conf.rb
@@ -79,6 +79,7 @@ after_mold_fork do |server, mold|
end
Discourse.redis.close
+ DiscourseVips::Client.use_shared_worker
Discourse.before_fork
end
@@ -102,6 +103,9 @@ end
before_service_worker_ready do |server, service_worker|
sidekiqs = ENV["UNICORN_SIDEKIQS"].to_i
+ require "demon/discourse_vips"
+ Demon::DiscourseVips.start(logger: server.logger) if GlobalSetting.enable_vips_image_processing
+
if sidekiqs > 0
server.logger.info "starting #{sidekiqs} supervised sidekiqs"
@@ -158,6 +162,8 @@ before_service_worker_ready do |server, service_worker|
Demon::Sidekiq.rss_memory_check
end
+ Demon::DiscourseVips.ensure_running if GlobalSetting.enable_vips_image_processing
+
DiscoursePluginRegistry.demon_processes.each { |demon_class| demon_class.ensure_running }
rescue => e
Rails.logger.warn(
diff --git a/docs/developer-guides/docs/02-development-environments/04-macos-setup.md b/docs/developer-guides/docs/02-development-environments/04-macos-setup.md
index fa54ecc0c23..2421bbc0196 100644
--- a/docs/developer-guides/docs/02-development-environments/04-macos-setup.md
+++ b/docs/developer-guides/docs/02-development-environments/04-macos-setup.md
@@ -24,6 +24,7 @@ You will need the following packages on your system:
- [Redis][redis_link]
- [Node.js][node_link]
- [pnpm][pnpm_link]
+- [libvips][libvips_link]
- [MailHog][mh_link]\*\*
- [ImageMagick][imagemagick_link]\*\*
@@ -139,6 +140,7 @@ Happy hacking! And to get started with that, see [Beginner’s Guide to Creating
[pg_link]: http://www.postgresql.org/
[sqlite_link]: https://sqlite.org/
[redis_link]: http://redis.io/
+[libvips_link]: https://github.com/libvips/libvips/wiki#building-and-installing
[imagemagick_link]: http://www.imagemagick.org/
[pnpm_link]: https://pnpm.io/
[mh_link]: https://github.com/mailhog/MailHog
diff --git a/docs/developer-guides/docs/02-development-environments/05-ubuntu-debian-setup.md b/docs/developer-guides/docs/02-development-environments/05-ubuntu-debian-setup.md
index a0192b031ef..5e2b0f4fb9a 100644
--- a/docs/developer-guides/docs/02-development-environments/05-ubuntu-debian-setup.md
+++ b/docs/developer-guides/docs/02-development-environments/05-ubuntu-debian-setup.md
@@ -40,6 +40,7 @@ You will need the following packages on your system:
- [Redis][redis_link]
- [Node.js][node_link]
- [pnpm][pnpm_link]
+- [libvips][libvips_link]
- [MailHog][mh_link]\*\*
- [ImageMagick][imagemagick_link]\*\*
@@ -145,6 +146,7 @@ Happy hacking! And to get started with that, see [Beginner’s Guide to Creating
[pg_link]: http://www.postgresql.org/
[sqlite_link]: https://sqlite.org/
[redis_link]: http://redis.io/
+[libvips_link]: https://github.com/libvips/libvips/wiki#building-and-installing
[imagemagick_link]: http://www.imagemagick.org/
[pnpm_link]: https://pnpm.io/
[mh_link]: https://github.com/mailhog/MailHog
diff --git a/docs/developer-guides/docs/02-development-environments/06-windows-10-setup.md b/docs/developer-guides/docs/02-development-environments/06-windows-10-setup.md
index 6958e3d9513..fc5ad161e35 100644
--- a/docs/developer-guides/docs/02-development-environments/06-windows-10-setup.md
+++ b/docs/developer-guides/docs/02-development-environments/06-windows-10-setup.md
@@ -34,6 +34,7 @@ You will need the following packages on your system:
- [Redis][redis_link]
- [Node.js][node_link]
- [pnpm][pnpm_link]
+- [libvips][libvips_link]
- [MailHog][mh_link]\*\*
- [ImageMagick][imagemagick_link]\*\*
@@ -511,6 +512,7 @@ _Last Reviewed by @SaraDev on [date=2022-06-15 time=19:00:00 timezone="America/L
[pg_link]: http://www.postgresql.org/
[sqlite_link]: https://sqlite.org/
[redis_link]: http://redis.io/
+[libvips_link]: https://github.com/libvips/libvips/wiki#building-and-installing
[imagemagick_link]: http://www.imagemagick.org/
[pnpm_link]: https://pnpm.io/
[mh_link]: https://github.com/mailhog/MailHog
diff --git a/docs/developer-guides/docs/02-development-environments/10-fedora-setup.md b/docs/developer-guides/docs/02-development-environments/10-fedora-setup.md
index 4324f7e8c2d..307cabea78a 100644
--- a/docs/developer-guides/docs/02-development-environments/10-fedora-setup.md
+++ b/docs/developer-guides/docs/02-development-environments/10-fedora-setup.md
@@ -12,7 +12,7 @@ If you're looking to install Discourse for a **production environment**, prefer
```sh
sudo dnf update
-sudo dnf install -y "@development-tools" git rpm-build zlib-devel ruby-devel readline-devel libpq-devel ImageMagick sqlite sqlite-devel nodejs npm curl gcc g++ bzip2 openssl-devel libyaml-devel libffi-devel zlib-devel gdbm-devel ncurses-devel optipng pngquant jhead jpegoptim gifsicle oxipng
+sudo dnf install -y "@development-tools" git rpm-build zlib-devel ruby-devel readline-devel libpq-devel ImageMagick vips sqlite sqlite-devel nodejs npm curl gcc g++ bzip2 openssl-devel libyaml-devel libffi-devel zlib-devel gdbm-devel ncurses-devel optipng pngquant jhead jpegoptim gifsicle oxipng
```
**Install required npm packages**
diff --git a/lib/demon/discourse_vips.rb b/lib/demon/discourse_vips.rb
new file mode 100644
index 00000000000..7c9c7521cbf
--- /dev/null
+++ b/lib/demon/discourse_vips.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require "demon/base"
+require "discourse_vips"
+
+class Demon::DiscourseVips < Demon::Base
+ def self.prefix
+ "discourse_vips"
+ end
+
+ def self.start(verbose: false, logger: nil)
+ ::DiscourseVips::Client.use_shared_worker
+ super(1, verbose:, logger:)
+ ::DiscourseVips.version
+ rescue StandardError
+ stop
+ raise
+ end
+
+ def self.release_inherited_worker
+ demons&.each_value(&:release_inherited_worker)
+ end
+
+ def run
+ @worker_process&.discard
+ @worker_process =
+ ::DiscourseVips::WorkerProcess.new(
+ socket_path: ::DiscourseVips::WorkerProcess.shared_socket_path,
+ )
+ @pid = @worker_process.pid
+ write_pid_file
+ rescue StandardError
+ @worker_process&.shutdown
+ @worker_process = nil
+ @pid = nil
+ @started = false
+ raise
+ end
+
+ def stop
+ worker_pid = @pid
+ @started = false
+ @worker_process&.shutdown
+ ensure
+ @worker_process = nil
+ @pid = nil
+ delete_pid_file(worker_pid)
+ end
+
+ def release_inherited_worker
+ @worker_process&.discard
+ @worker_process = nil
+ end
+
+ private
+
+ def delete_pid_file(worker_pid)
+ return if !worker_pid || !File.exist?(pid_file)
+ return if File.read(pid_file).to_i != worker_pid
+
+ File.delete(pid_file)
+ rescue SystemCallError
+ end
+end
diff --git a/lib/discourse.rb b/lib/discourse.rb
index c0387760b17..9f9494f8065 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -1047,6 +1047,8 @@ module Discourse
# before forking, otherwise the forked process might
# be in a bad state
def self.before_fork
+ DiscourseVips.before_fork
+
if GlobalSetting.mini_racer_single_threaded
ObjectSpace.each_object(MiniRacer::Context) { |c| c.low_memory_notification }
else
@@ -1086,6 +1088,8 @@ module Discourse
# after fork, otherwise Discourse will be
# in a bad state
def self.after_fork
+ Demon::DiscourseVips.release_inherited_worker if defined?(Demon::DiscourseVips)
+
# note: some of this reconnecting may no longer be needed per https://github.com/redis/redis-rb/pull/414
MessageBus.after_fork
SiteSetting.after_fork
@@ -1328,7 +1332,7 @@ module Discourse
ActionviewPrecompiler.precompile
end,
Thread.new do
- LetterAvatar.image_magick_version
+ LetterAvatar.version
LetterAvatar.cleanup_old
end,
Thread.new { SvgSprite.core_svgs },
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
new file mode 100644
index 00000000000..c5a50d975f8
--- /dev/null
+++ b/lib/discourse_vips.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+require_relative "discourse_vips/client"
+
+module DiscourseVips
+ def self.version
+ Client.call(["version"], operation: :vips_version)
+ end
+
+ def self.letter_avatar(letter:, output_path:, background_color:, font:, font_path:)
+ Client.call(
+ ["letter-avatar", letter, output_path, background_color, font, font_path],
+ operation: :letter_avatar_render,
+ )
+ end
+
+ def self.dominant_color(input_path:, timeout:)
+ Client.call(
+ ["dominant-color", input_path],
+ operation: :upload_dominant_color,
+ timeout:,
+ nice: 10,
+ )
+ end
+
+ def self.before_fork
+ Client.before_fork
+ end
+end
diff --git a/lib/discourse_vips/client.rb b/lib/discourse_vips/client.rb
new file mode 100644
index 00000000000..a362675f0d8
--- /dev/null
+++ b/lib/discourse_vips/client.rb
@@ -0,0 +1,161 @@
+# frozen_string_literal: true
+
+require "image_processing/instrumentation"
+require "msgpack"
+require "socket"
+
+require_relative "worker_process"
+
+module DiscourseVips
+ module Client
+ DEFAULT_TIMEOUT_SECONDS = 30
+ private_constant :DEFAULT_TIMEOUT_SECONDS
+
+ WORKER_GRACE_SECONDS = 2
+ private_constant :WORKER_GRACE_SECONDS
+
+ @owner_pid = Process.pid
+ @shared_worker = false
+ @worker_process_mutex = Mutex.new
+
+ def self.use_shared_worker
+ @shared_worker = true
+ reset_worker_process
+ end
+
+ def self.call(command, operation:, timeout: DEFAULT_TIMEOUT_SECONDS, nice: nil)
+ payload = { command: command.map(&:to_s), timeout:, nice: }
+
+ ImageProcessing::Instrumentation.instrument(operation:) do
+ response = send_command(payload, timeout:)
+ case response["status"]
+ when "ok"
+ response["value"]
+ when "timeout"
+ raise OperationTimeout, "libvips operation timed out"
+ when "invalid_image"
+ raise InvalidImage, response["message"].presence || "invalid image"
+ else
+ raise Error, response["message"].presence || "libvips operation failed"
+ end
+ end
+ end
+
+ def self.before_fork
+ reset_worker_process
+ end
+
+ def self.send_command(request, timeout:)
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout + WORKER_GRACE_SECONDS
+ socket = connect_socket(deadline:)
+
+ write_request(socket, MessagePack.pack(request), deadline:)
+ socket.close_write
+
+ payload = read_response(socket, deadline:)
+ raise WorkerUnavailable, "libvips worker returned no response" if payload.empty?
+
+ MessagePack.unpack(payload)
+ rescue WorkerUnavailable
+ reset_worker_process if !@shared_worker
+ raise
+ rescue MessagePack::UnpackError, IOError, SystemCallError => error
+ reset_worker_process if !@shared_worker
+
+ raise WorkerUnavailable, "libvips worker request failed: #{error.message}"
+ ensure
+ socket&.close unless socket&.closed?
+ end
+ private_class_method :send_command
+
+ def self.connect_socket(deadline:)
+ Addrinfo.unix(worker_socket_path).connect(timeout: remaining_time(deadline))
+ rescue IO::TimeoutError
+ raise WorkerUnavailable, "libvips worker did not respond"
+ end
+ private_class_method :connect_socket
+
+ def self.write_request(socket, request, deadline:)
+ offset = 0
+ while offset < request.bytesize
+ written = socket.write_nonblock(request.byteslice(offset..), exception: false)
+ if written == :wait_writable
+ wait_for_socket(socket, deadline:, readable: false)
+ else
+ offset += written
+ end
+ end
+ end
+ private_class_method :write_request
+
+ def self.read_response(socket, deadline:)
+ response = +""
+ loop do
+ chunk = socket.read_nonblock(16 * 1024, exception: false)
+ return response if chunk.nil?
+
+ if chunk == :wait_readable
+ wait_for_socket(socket, deadline:, readable: true)
+ else
+ response << chunk
+ end
+ end
+ end
+ private_class_method :read_response
+
+ def self.wait_for_socket(socket, deadline:, readable:)
+ timeout = remaining_time(deadline)
+ ready =
+ readable ? IO.select([socket], nil, nil, timeout) : IO.select(nil, [socket], nil, timeout)
+
+ raise WorkerUnavailable, "libvips worker did not respond" if !ready
+ end
+ private_class_method :wait_for_socket
+
+ def self.remaining_time(deadline)
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ raise WorkerUnavailable, "libvips worker did not respond" if !remaining.positive?
+
+ remaining
+ end
+ private_class_method :remaining_time
+
+ def self.worker_socket_path
+ return WorkerProcess.shared_socket_path if @shared_worker
+
+ worker_process.socket_path
+ end
+ private_class_method :worker_socket_path
+
+ def self.worker_process
+ reset_after_fork if @owner_pid != Process.pid
+
+ @worker_process_mutex.synchronize do
+ if @worker_process && !@worker_process.alive?
+ @worker_process.shutdown
+ @worker_process = nil
+ end
+ @worker_process ||= WorkerProcess.new
+ end
+ rescue SystemCallError => error
+ raise WorkerUnavailable, "libvips worker could not start: #{error.message}"
+ end
+ private_class_method :worker_process
+
+ def self.reset_after_fork
+ @worker_process&.discard
+ @owner_pid = Process.pid
+ @worker_process = nil
+ @worker_process_mutex = Mutex.new
+ end
+ private_class_method :reset_after_fork
+
+ def self.reset_worker_process
+ reset_after_fork if @owner_pid != Process.pid
+ worker_process =
+ @worker_process_mutex.synchronize { @worker_process.tap { @worker_process = nil } }
+ worker_process&.shutdown
+ end
+ private_class_method :reset_worker_process
+ end
+end
diff --git a/lib/discourse_vips/worker_process.rb b/lib/discourse_vips/worker_process.rb
new file mode 100644
index 00000000000..627954d0e94
--- /dev/null
+++ b/lib/discourse_vips/worker_process.rb
@@ -0,0 +1,172 @@
+# frozen_string_literal: true
+
+require "fileutils"
+require "rbconfig"
+require "socket"
+require "tmpdir"
+
+module DiscourseVips
+ class Error < RuntimeError
+ end
+
+ class WorkerUnavailable < Error
+ end
+
+ class OperationTimeout < Error
+ end
+
+ class InvalidImage < Error
+ end
+
+ class WorkerProcess
+ WORKER_GRACE_SECONDS = 2
+ private_constant :WORKER_GRACE_SECONDS
+
+ attr_reader :pid, :socket_path
+
+ def self.shared_socket_path
+ Rails.root.join("tmp", "discourse-vips-worker", Rails.env, "socket").to_s
+ end
+
+ def initialize(socket_path: nil)
+ @state_mutex = Mutex.new
+ @socket_path = socket_path
+ start
+ end
+
+ def alive?
+ @state_mutex.synchronize do
+ return false if @closed
+
+ Process.waitpid(@pid, Process::WNOHANG).nil?
+ rescue Errno::ECHILD
+ false
+ end
+ end
+
+ def shutdown
+ worker_pid, owner_writer =
+ @state_mutex.synchronize do
+ return if @closed
+
+ @closed = true
+ [@pid, @owner_writer]
+ end
+
+ owner_writer.close unless owner_writer.closed?
+ wait_for_worker(worker_pid)
+ rescue IOError, Errno::ECHILD
+ ensure
+ remove_socket
+ end
+
+ def discard
+ @state_mutex.synchronize do
+ return if @closed
+
+ @closed = true
+ @owner_writer.close unless @owner_writer.closed?
+ end
+ rescue IOError
+ end
+
+ private
+
+ def start
+ prepare_socket_path
+ server = UNIXServer.new(@socket_path)
+ File.chmod(0o600, @socket_path)
+ @socket_identity = File.stat(@socket_path).then { |stat| [stat.dev, stat.ino] }
+ owner_reader, @owner_writer = IO.pipe
+
+ @pid =
+ Process.spawn(
+ worker_environment,
+ *worker_command,
+ server.fileno.to_s,
+ owner_reader.fileno.to_s,
+ server.fileno => server,
+ owner_reader.fileno => owner_reader,
+ :in => File::NULL,
+ :close_others => true,
+ :pgroup => true,
+ :unsetenv_others => true,
+ )
+ @closed = false
+ rescue Exception
+ @owner_writer&.close
+ remove_socket
+ raise
+ ensure
+ server&.close
+ owner_reader&.close
+ end
+
+ def prepare_socket_path
+ if @socket_path
+ socket_directory = File.dirname(@socket_path)
+ FileUtils.mkdir_p(socket_directory, mode: 0o700)
+ File.chmod(0o700, socket_directory)
+ FileUtils.rm_f(@socket_path)
+ else
+ socket_directory = Dir.mktmpdir("discourse-vips-worker-", Rails.root.join("tmp").to_s)
+ @socket_path = File.join(socket_directory, "socket")
+ end
+ end
+
+ def worker_command
+ load_paths = [Rails.root.join("lib").to_s]
+ %w[ffi landlock msgpack ruby-vips].each do |gem_name|
+ load_paths.concat(Gem.loaded_specs.fetch(gem_name).full_require_paths)
+ end
+
+ [
+ RbConfig.ruby,
+ "--disable-gems",
+ *load_paths.uniq.flat_map { |path| ["-I", path] },
+ Rails.root.join("script/discourse_vips_worker").to_s,
+ ]
+ end
+
+ def worker_environment
+ {
+ **ENV.to_h.slice("PATH", "LANG", "LC_ALL"),
+ "HOME" => "/tmp",
+ "MALLOC_ARENA_MAX" => "2",
+ "TMPDIR" => "/tmp",
+ "XDG_CACHE_HOME" => "/tmp",
+ }
+ end
+
+ def wait_for_worker(worker_pid)
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + WORKER_GRACE_SECONDS
+ while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
+ return if Process.waitpid(worker_pid, Process::WNOHANG)
+
+ sleep 0.01
+ end
+
+ terminate_worker_group(worker_pid)
+ Process.waitpid(worker_pid)
+ rescue Errno::ECHILD, Errno::ESRCH
+ ensure
+ terminate_worker_group(worker_pid)
+ end
+
+ def terminate_worker_group(worker_pid)
+ Process.kill("KILL", -worker_pid)
+ rescue Errno::ESRCH
+ end
+
+ def remove_socket
+ if File.exist?(@socket_path)
+ stat = File.stat(@socket_path)
+ return if [stat.dev, stat.ino] != @socket_identity
+
+ File.unlink(@socket_path)
+ end
+ Dir.rmdir(File.dirname(@socket_path))
+ rescue SystemCallError
+ end
+ end
+end
diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb
index ad5f0e5c73c..8a01bb5f9b9 100644
--- a/lib/letter_avatar.rb
+++ b/lib/letter_avatar.rb
@@ -18,13 +18,25 @@ class LetterAvatar
# BUMP UP if avatar algorithm changes
VERSION = 5
+ VIPS_VERSION = 6
+ private_constant :VIPS_VERSION
+
# CHANGE these values to support more pixel ratios
FULLSIZE = 120 * 3
POINTSIZE = 280
+ DISCOURSE_FONT_PATH = File.join(DiscourseFonts.path_for_fonts, "NotoSans-Regular.woff2")
+ private_constant :DISCOURSE_FONT_PATH
+
+ MACOS_FONT_PATH = "/System/Library/Fonts/Helvetica.ttc"
+ private_constant :MACOS_FONT_PATH
class << self
def version
- "#{VERSION}_#{image_magick_version}"
+ if GlobalSetting.enable_vips_image_processing
+ "#{VIPS_VERSION}_#{vips_version}"
+ else
+ "#{VERSION}_#{image_magick_version}"
+ end
end
def cache_path
@@ -67,49 +79,46 @@ class LetterAvatar
end
def generate_fullsize(identity)
- color = identity.color
- letter = identity.letter
-
filename = fullsize_path(identity)
- # Use NimbusSans-Regular, except for macOS where it is unavailable, use Helvetica there
- font = RbConfig::CONFIG["host_os"].match?(/darwin/i) ? "Helvetica" : "NimbusSans-Regular"
- # and adjust vertical offset accordingly
- vertical_offset = font == "Helvetica" ? 26 : 34
-
- instructions = %W[
- -size
- #{FULLSIZE}x#{FULLSIZE}
- xc:#{to_rgb(color)}
- -pointsize
- #{POINTSIZE}
- -fill
- #FFFFFFCC
- -font
- #{font}
- -gravity
- Center
- -annotate
- -0+#{vertical_offset}
- #{letter}
- -depth
- 8
- #{filename}
- ]
-
- ImageMagick.magick(
- *instructions,
- operation: :letter_avatar_render,
- write: [File.dirname(filename)],
- )
-
- ## do not optimize image, it will end up larger than original
- filename
- end
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.letter_avatar(
+ letter: ERB::Util.html_escape(identity.letter),
+ output_path: filename,
+ background_color: format("%02X%02X%02X", *identity.color),
+ font: "#{font_family} #{POINTSIZE}",
+ font_path:,
+ )
+ else
+ color = identity.color
+ letter = identity.letter
+ font = macos? ? "Helvetica" : "NimbusSans-Regular"
+ vertical_offset = font == "Helvetica" ? 26 : 34
- def to_rgb(color)
- r, g, b = color
- "rgb(#{r},#{g},#{b})"
+ ImageMagick.magick(
+ "-size",
+ "#{FULLSIZE}x#{FULLSIZE}",
+ "xc:#{to_rgb(color)}",
+ "-pointsize",
+ POINTSIZE.to_s,
+ "-fill",
+ "#FFFFFFCC",
+ "-font",
+ font,
+ "-gravity",
+ "Center",
+ "-annotate",
+ "-0+#{vertical_offset}",
+ letter,
+ "-depth",
+ "8",
+ filename,
+ operation: :letter_avatar_render,
+ write: [File.dirname(filename)],
+ )
+ end
+
+ filename
end
def image_magick_version
@@ -123,6 +132,12 @@ class LetterAvatar
)
end
+ def vips_version
+ return @vips_version if @vips_version
+
+ @vips_version = Digest::MD5.hexdigest([DiscourseVips.version, font_version].join("\0"))
+ end
+
def cleanup_old
skip = File.basename(cache_path)
parent_path = File.dirname(cache_path)
@@ -134,6 +149,29 @@ class LetterAvatar
rescue Errno::ENOENT
# no worries, folder doesn't exists
end
+
+ private
+
+ def to_rgb(color)
+ red, green, blue = color
+ "rgb(#{red},#{green},#{blue})"
+ end
+
+ def font_path
+ macos? ? MACOS_FONT_PATH : DISCOURSE_FONT_PATH
+ end
+
+ def font_version
+ Digest::MD5.file(font_path).hexdigest
+ end
+
+ def macos?
+ RbConfig::CONFIG["host_os"].match?(/darwin/i)
+ end
+
+ def font_family
+ macos? ? "Helvetica" : "Noto Sans"
+ end
end
# palette of optimally distinct colors
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
new file mode 100644
index 00000000000..fe1a5f56027
--- /dev/null
+++ b/script/discourse_vips_worker
@@ -0,0 +1,331 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+
+require "landlock"
+require "msgpack"
+require "socket"
+require "tmpdir"
+require "vips"
+
+raise LoadError, "libvips 8.13 or newer is required" if !Vips.at_least_libvips?(8, 13)
+
+class DiscourseVipsWorker
+ class InvalidImage < StandardError
+ end
+
+ LINUX_PLATFORM = RUBY_PLATFORM.include?("linux")
+ private_constant :LINUX_PLATFORM
+
+ class Operations
+ DEFAULT_READ_PATHS = %w[/bin /lib /lib64 /usr].freeze
+ private_constant :DEFAULT_READ_PATHS
+
+ # Dynamically loaded libvips modules use this cache to locate their shared libraries.
+ DYNAMIC_LINKER_CACHE_PATH = "/etc/ld.so.cache"
+ private_constant :DYNAMIC_LINKER_CACHE_PATH
+
+ WARM_OPERATIONS = %w[text gravity flatten pngsave thumbnail].freeze
+ private_constant :WARM_OPERATIONS
+
+ def self.warm
+ WARM_OPERATIONS.each do |name|
+ operation = Vips.vips_operation_new(name)
+ raise "unable to initialize libvips operation #{name}" if operation.null?
+
+ GObject.g_object_unref(operation)
+ end
+ end
+
+ def self.build(command)
+ operation, *arguments = command
+
+ case operation
+ when "version"
+ version
+ when "letter-avatar"
+ letter_avatar(*arguments)
+ when "dominant-color"
+ dominant_color(*arguments)
+ else
+ raise ArgumentError, "unsupported libvips operation"
+ end
+ end
+
+ def initialize(read_paths: [], write_paths: [], &action)
+ @read_paths = read_paths
+ @write_paths = write_paths
+ @action = action
+ end
+
+ def call
+ @action.call
+ end
+
+ def sandbox_paths(scratch:)
+ {
+ "read" => existing_paths([*DEFAULT_READ_PATHS, DYNAMIC_LINKER_CACHE_PATH, *@read_paths]),
+ "write" => existing_paths([scratch, *@write_paths]),
+ }
+ end
+
+ def self.version
+ new { Vips.version_string }
+ end
+ private_class_method :version
+
+ def self.letter_avatar(letter, output_path, background_color, font, font_path)
+ new(
+ read_paths: ["/etc/fonts", "/var/cache/fontconfig", font_path],
+ write_paths: [File.dirname(output_path)],
+ ) do
+ if !letter || !output_path || !font_path
+ raise ArgumentError, "invalid letter avatar arguments"
+ end
+ raise ArgumentError, "font file does not exist" if !File.file?(font_path)
+ raise ArgumentError, "invalid background color" if !background_color.match?(/\A\h{6}\z/)
+
+ background = background_color.scan(/\h{2}/).map { |channel| Integer(channel, 16) }
+
+ block_loaders
+ markup = %(<span foreground="#ffffff" alpha="80%">#{letter}</span>)
+ text = Vips::Image.text(markup, font:, dpi: 72, fontfile: font_path, rgba: true)
+ canvas =
+ text.gravity(:centre, 360, 360, extend: :background, background: [*background, 255])
+ canvas.flatten(background:).pngsave(output_path, compression: 6)
+ nil
+ end
+ end
+ private_class_method :letter_avatar
+
+ def self.dominant_color(input_path)
+ new(read_paths: [input_path]) do
+ raise ArgumentError, "invalid dominant color arguments" if !input_path
+
+ block_loaders(
+ allowed: %w[
+ VipsForeignLoadJpeg
+ VipsForeignLoadPng
+ VipsForeignLoadNsgif
+ VipsForeignLoadWebp
+ VipsForeignLoadHeif
+ VipsForeignLoadJxl
+ ],
+ )
+ components =
+ begin
+ image = Vips::Image.new_from_file(input_path, access: :sequential)
+ if image.has_alpha?
+ alpha_weighted_components(image)
+ elsif %i[float double].include?(image.format)
+ normalize_color(image)
+ .thumbnail_image(1, height: 1, size: :force)
+ .getpoint(0, 0)
+ .first(3)
+ .map(&:round)
+ else
+ thumbnail = Vips::Image.thumbnail(input_path, 1, height: 1, size: :force)
+ normalize_color(thumbnail).getpoint(0, 0).first(3).map(&:round)
+ end
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
+ format("%02X%02X%02X", *components)
+ end
+ end
+ private_class_method :dominant_color
+
+ def self.normalize_color(image)
+ image = image.colourspace(:srgb)
+ image = (image * 255).round(:rint) if %i[float double].include?(image.format)
+ image.cast(:uchar, shift: true)
+ end
+ private_class_method :normalize_color
+
+ def self.alpha_weighted_components(image)
+ alpha = image[image.bands - 1]
+ colors = normalize_color(image.extract_band(0, n: image.bands - 1))
+ weighted_colors = colors * alpha
+ statistics = alpha.bandjoin(weighted_colors).stats
+ alpha_sum = statistics.getpoint(2, 1).first
+ return 0, 0, 0 if alpha_sum.zero?
+
+ 3.times.map { |band| (statistics.getpoint(2, band + 2).first / alpha_sum).round }
+ end
+ private_class_method :alpha_weighted_components
+
+ def self.block_loaders(allowed: [])
+ Vips.block_untrusted(true)
+ Vips.block("VipsForeignLoad", true)
+ allowed.each { |loader| Vips.block(loader, false) }
+ end
+ private_class_method :block_loaders
+
+ private
+
+ def existing_paths(paths)
+ paths
+ .filter { |path| path.to_s != "" && File.exist?(path) }
+ .map { |path| File.expand_path(path) }
+ .uniq
+ end
+ end
+
+ RLIMITS = {
+ cpu_seconds: 300,
+ memory_bytes: 4 * 1024 * 1024 * 1024,
+ file_size_bytes: 10 * 1024 * 1024 * 1024,
+ open_files: 1024,
+ }.freeze
+ private_constant :RLIMITS
+
+ def self.run(server_fd:, owner_fd:)
+ parent_pid = Process.ppid
+ Landlock::Native.set_parent_death_signal! if LINUX_PLATFORM
+ exit! 1 if Process.ppid != parent_pid
+
+ Process.setproctitle("discourse vips worker")
+ new(server: UNIXServer.for_fd(server_fd), owner_reader: IO.for_fd(owner_fd)).run
+ end
+
+ def initialize(server:, owner_reader:)
+ @server = server
+ @socket_path = server.local_address.unix_path
+ @socket_identity = File.stat(@socket_path).then { |stat| [stat.dev, stat.ino] }
+ @owner_reader = owner_reader
+ end
+
+ def run
+ Signal.trap("CHLD") { reap_connection_processes }
+ Operations.warm
+
+ loop do
+ ready_ios = IO.select([@server, @owner_reader]).first
+ break if ready_ios.include?(@owner_reader)
+
+ connection = @server.accept
+ if LINUX_PLATFORM
+ fork_connection_process(connection)
+ else
+ handle_connection(connection)
+ end
+ end
+ ensure
+ @server.close unless @server.closed?
+ @owner_reader.close unless @owner_reader.closed?
+ remove_socket_directory
+ Process.kill("KILL", -Process.pid)
+ end
+
+ private
+
+ def fork_connection_process(connection)
+ parent_pid = Process.pid
+ fork do
+ Landlock::Native.set_parent_death_signal! if LINUX_PLATFORM
+ exit! 1 if Process.ppid != parent_pid
+ Signal.trap("CHLD", "DEFAULT")
+ @server.close
+ @owner_reader.close
+ handle_connection(connection)
+ exit! 0
+ end
+
+ connection.close
+ end
+
+ def reap_connection_processes
+ while Process.waitpid(-1, Process::WNOHANG)
+ end
+ rescue Errno::ECHILD
+ end
+
+ def handle_connection(connection)
+ request = MessagePack.unpack(connection.read)
+ operation = Operations.build(request.fetch("command"))
+ response =
+ if LINUX_PLATFORM
+ run_sandboxed(request:, operation:)
+ else
+ run_direct(operation)
+ end
+ write_response(connection, response)
+ rescue StandardError => error
+ write_response(connection, "status" => "error", "message" => error.message.byteslice(0, 4096))
+ ensure
+ connection.close unless connection.closed?
+ end
+
+ def write_response(connection, response)
+ connection.write(MessagePack.pack(response))
+ rescue IOError, SystemCallError
+ end
+
+ def run_sandboxed(request:, operation:)
+ Dir.mktmpdir("discourse-vips-") do |scratch|
+ sandbox_paths = operation.sandbox_paths(scratch:)
+ result =
+ Landlock.fork(
+ read: sandbox_paths.fetch("read"),
+ write: sandbox_paths.fetch("write"),
+ execute: [],
+ timeout: Float(request.fetch("timeout")),
+ env: child_environment(scratch),
+ unsetenv_others: true,
+ rlimits: RLIMITS,
+ seccomp_deny_network: true,
+ on_unsupported: :run_without_landlock,
+ ) do |stdout, _stderr|
+ apply_nice(request)
+ stdout.write(MessagePack.pack(operation_response(operation)))
+ end
+
+ if result.timed_out?
+ { "status" => "timeout" }
+ elsif result.success?
+ MessagePack.unpack(result.stdout)
+ else
+ { "status" => "error", "message" => "libvips operation failed" }
+ end
+ end
+ end
+
+ def run_direct(operation)
+ operation_response(operation)
+ end
+
+ def remove_socket_directory
+ stat = File.stat(@socket_path)
+ return if [stat.dev, stat.ino] != @socket_identity
+
+ File.unlink(@socket_path)
+ Dir.rmdir(File.dirname(@socket_path))
+ rescue SystemCallError
+ end
+
+ def child_environment(scratch)
+ {
+ "HOME" => scratch,
+ "TMPDIR" => scratch,
+ "XDG_CACHE_HOME" => scratch,
+ "MALLOC_ARENA_MAX" => "2",
+ }
+ end
+
+ def apply_nice(request)
+ Process.setpriority(Process::PRIO_PROCESS, 0, Integer(request.fetch("nice"))) if request["nice"]
+ end
+
+ def operation_response(operation)
+ value = operation.call
+ { "status" => "ok", "value" => value }
+ rescue InvalidImage => error
+ { "status" => "invalid_image", "message" => error.message.byteslice(0, 4096) }
+ rescue StandardError => error
+ { "status" => "error", "message" => error.message.byteslice(0, 4096) }
+ end
+end
+
+if $PROGRAM_NAME == __FILE__
+ server_fd, owner_fd = ARGV.map { |argument| Integer(argument) }
+ DiscourseVipsWorker.run(server_fd:, owner_fd:)
+end
diff --git a/script/letter_avatar_pixel_diff b/script/letter_avatar_pixel_diff
index b7c3db602fa..c19ac7e6656 100755
--- a/script/letter_avatar_pixel_diff
+++ b/script/letter_avatar_pixel_diff
@@ -5,6 +5,8 @@ ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
+GlobalSetting.define_singleton_method(:enable_vips_image_processing) { true }
+
require "base64"
require "chunky_png"
require "fileutils"
diff --git a/spec/fixtures/images/dominant-color-float.jxl b/spec/fixtures/images/dominant-color-float.jxl
new file mode 100644
index 00000000000..1d8885f8149
Binary files /dev/null and b/spec/fixtures/images/dominant-color-float.jxl differ
diff --git a/spec/fixtures/images/dominant-color-hidden-rgb.png b/spec/fixtures/images/dominant-color-hidden-rgb.png
new file mode 100644
index 00000000000..164e8ced3ff
Binary files /dev/null and b/spec/fixtures/images/dominant-color-hidden-rgb.png differ
diff --git a/spec/fixtures/images/dominant-color-low-alpha-16bit.png b/spec/fixtures/images/dominant-color-low-alpha-16bit.png
new file mode 100644
index 00000000000..ee94bd624ec
Binary files /dev/null and b/spec/fixtures/images/dominant-color-low-alpha-16bit.png differ
diff --git a/spec/fixtures/images/dominant-color-semitransparent.png b/spec/fixtures/images/dominant-color-semitransparent.png
new file mode 100644
index 00000000000..bb2cec7dda9
Binary files /dev/null and b/spec/fixtures/images/dominant-color-semitransparent.png differ
diff --git a/spec/fixtures/images/dominant-color-transparent.png b/spec/fixtures/images/dominant-color-transparent.png
new file mode 100644
index 00000000000..e54d8050140
Binary files /dev/null and b/spec/fixtures/images/dominant-color-transparent.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/A.png b/spec/fixtures/images/letter_avatar_visual/linux/A.png
index f03ee92d190..5875eaa03be 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/A.png and b/spec/fixtures/images/letter_avatar_visual/linux/A.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/B.png b/spec/fixtures/images/letter_avatar_visual/linux/B.png
index e176d189cdf..f2055fefa4d 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/B.png and b/spec/fixtures/images/letter_avatar_visual/linux/B.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/C.png b/spec/fixtures/images/letter_avatar_visual/linux/C.png
index c1308e1633e..5032aa7ace6 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/C.png and b/spec/fixtures/images/letter_avatar_visual/linux/C.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/D.png b/spec/fixtures/images/letter_avatar_visual/linux/D.png
index 9b7e10d9307..581c43fc50b 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/D.png and b/spec/fixtures/images/letter_avatar_visual/linux/D.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/E.png b/spec/fixtures/images/letter_avatar_visual/linux/E.png
index a34f9003200..f9aca7b8483 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/E.png and b/spec/fixtures/images/letter_avatar_visual/linux/E.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/F.png b/spec/fixtures/images/letter_avatar_visual/linux/F.png
index f0f646e69db..8241cf8aae3 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/F.png and b/spec/fixtures/images/letter_avatar_visual/linux/F.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/G.png b/spec/fixtures/images/letter_avatar_visual/linux/G.png
index 036015aaa95..b1d8c01e9bf 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/G.png and b/spec/fixtures/images/letter_avatar_visual/linux/G.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/H.png b/spec/fixtures/images/letter_avatar_visual/linux/H.png
index 762a5dd5899..3fd6b13ed7a 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/H.png and b/spec/fixtures/images/letter_avatar_visual/linux/H.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/I.png b/spec/fixtures/images/letter_avatar_visual/linux/I.png
index 832fb97b75e..1fde98cb1d2 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/I.png and b/spec/fixtures/images/letter_avatar_visual/linux/I.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/J.png b/spec/fixtures/images/letter_avatar_visual/linux/J.png
index be4633e33ac..6973ed7ff05 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/J.png and b/spec/fixtures/images/letter_avatar_visual/linux/J.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/K.png b/spec/fixtures/images/letter_avatar_visual/linux/K.png
index 90a06966c68..502746bff4e 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/K.png and b/spec/fixtures/images/letter_avatar_visual/linux/K.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/L.png b/spec/fixtures/images/letter_avatar_visual/linux/L.png
index 2c264c77273..725cbbce6c6 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/L.png and b/spec/fixtures/images/letter_avatar_visual/linux/L.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/M.png b/spec/fixtures/images/letter_avatar_visual/linux/M.png
index ebe92022085..5ae674263e5 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/M.png and b/spec/fixtures/images/letter_avatar_visual/linux/M.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/N.png b/spec/fixtures/images/letter_avatar_visual/linux/N.png
index 1da40345e08..6f5fd010967 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/N.png and b/spec/fixtures/images/letter_avatar_visual/linux/N.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/O.png b/spec/fixtures/images/letter_avatar_visual/linux/O.png
index cac47b653cf..1d93d24c151 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/O.png and b/spec/fixtures/images/letter_avatar_visual/linux/O.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/P.png b/spec/fixtures/images/letter_avatar_visual/linux/P.png
index 43a3333e402..11b9ec6e811 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/P.png and b/spec/fixtures/images/letter_avatar_visual/linux/P.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Q.png b/spec/fixtures/images/letter_avatar_visual/linux/Q.png
index a775c6b225c..89cce65ae65 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/Q.png and b/spec/fixtures/images/letter_avatar_visual/linux/Q.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/R.png b/spec/fixtures/images/letter_avatar_visual/linux/R.png
index f1588acfb79..db64fffba86 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/R.png and b/spec/fixtures/images/letter_avatar_visual/linux/R.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/S.png b/spec/fixtures/images/letter_avatar_visual/linux/S.png
index f37f8d8230a..62465e1a97f 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/S.png and b/spec/fixtures/images/letter_avatar_visual/linux/S.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/T.png b/spec/fixtures/images/letter_avatar_visual/linux/T.png
index 14d0735e1e9..bd77bd225f2 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/T.png and b/spec/fixtures/images/letter_avatar_visual/linux/T.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/U.png b/spec/fixtures/images/letter_avatar_visual/linux/U.png
index fbcf3ae6339..36e9221081e 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/U.png and b/spec/fixtures/images/letter_avatar_visual/linux/U.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/V.png b/spec/fixtures/images/letter_avatar_visual/linux/V.png
index 897fbfed013..9e5da58ca8a 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/V.png and b/spec/fixtures/images/letter_avatar_visual/linux/V.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/W.png b/spec/fixtures/images/letter_avatar_visual/linux/W.png
index 9c59ccd6458..64ea7861244 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/W.png and b/spec/fixtures/images/letter_avatar_visual/linux/W.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/X.png b/spec/fixtures/images/letter_avatar_visual/linux/X.png
index 669bbdbd9ee..67549496b29 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/X.png and b/spec/fixtures/images/letter_avatar_visual/linux/X.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Y.png b/spec/fixtures/images/letter_avatar_visual/linux/Y.png
index ef515d4d7e7..360bc7e5423 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/Y.png and b/spec/fixtures/images/letter_avatar_visual/linux/Y.png differ
diff --git a/spec/fixtures/images/letter_avatar_visual/linux/Z.png b/spec/fixtures/images/letter_avatar_visual/linux/Z.png
index b750847f9b8..8375c703759 100644
Binary files a/spec/fixtures/images/letter_avatar_visual/linux/Z.png and b/spec/fixtures/images/letter_avatar_visual/linux/Z.png differ
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
new file mode 100644
index 00000000000..8221d7c7b2f
--- /dev/null
+++ b/spec/lib/discourse_vips_spec.rb
@@ -0,0 +1,112 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseVips do
+ describe ".version" do
+ it "returns the libvips version" do
+ expect(described_class.version).to match(/\A\d+\.\d+\.\d+\z/)
+ end
+ end
+
+ describe ".dominant_color" do
+ def dominant_color(filename)
+ described_class.dominant_color(input_path: file_from_fixtures(filename).path, timeout: 5)
+ end
+
+ it "ignores RGB values in fully transparent pixels" do
+ expect(dominant_color("dominant-color-hidden-rgb.png")).to eq("FF0000")
+ end
+
+ it "weights partially transparent pixels by their alpha" do
+ expect(dominant_color("dominant-color-semitransparent.png")).to eq("AA0055")
+ end
+
+ it "returns black for a fully transparent image" do
+ expect(dominant_color("dominant-color-transparent.png")).to eq("000000")
+ end
+
+ it "preserves low nonzero alpha values in 16-bit images" do
+ expect(dominant_color("dominant-color-low-alpha-16bit.png")).to eq("FF0000")
+ end
+
+ it "normalizes floating-point grayscale JXL color values" do
+ expect(dominant_color("dominant-color-float.jxl")).to eq("808080")
+ end
+ end
+
+ describe "worker lifecycle" do
+ it "recovers after the worker exits unexpectedly" do
+ described_class.version
+ worker_pid =
+ Integer(
+ IO.popen(["pgrep", "-P", Process.pid.to_s, "-f", "discourse vips worker"]) do |process|
+ process.read
+ end,
+ 10,
+ )
+
+ Process.kill("KILL", worker_pid)
+ Process.waitpid(worker_pid)
+
+ expect(described_class.version).to match(/\A\d+\.\d+\.\d+\z/)
+ end
+
+ it "recovers after an operation times out" do
+ skip "per-operation timeouts require fork isolation" if RUBY_PLATFORM.include?("darwin")
+
+ Dir.mktmpdir do |directory|
+ input_path = File.join(directory, "blocked.png")
+ File.mkfifo(input_path)
+
+ File.open(input_path, File::RDWR) do
+ expect { described_class.dominant_color(input_path:, timeout: 0.05) }.to raise_error(
+ DiscourseVips::OperationTimeout,
+ "libvips operation timed out",
+ )
+ end
+ end
+
+ expect(described_class.version).to match(/\A\d+\.\d+\.\d+\z/)
+ end
+
+ it "times out when the worker sends an incomplete response" do
+ Dir.mktmpdir do |directory|
+ socket_path = File.join(directory, "socket")
+ server = UNIXServer.new(socket_path)
+ server_thread =
+ Thread.new do
+ connection = server.accept
+ connection.read
+ connection.write("\x81")
+ sleep 3
+ ensure
+ connection&.close
+ end
+
+ DiscourseVips::Client.stubs(:worker_socket_path).returns(socket_path)
+
+ expect {
+ described_class.dominant_color(input_path: "unused", timeout: 0.01)
+ }.to raise_error(DiscourseVips::WorkerUnavailable, "libvips worker did not respond")
+ ensure
+ server&.close
+ server_thread&.kill
+ server_thread&.join
+ end
+ end
+ end
+
+ it "records image-processing instrumentation" do
+ SiteSetting.instrument_image_processing = true
+ input_path = file_from_fixtures("cropped.png").path
+
+ events =
+ DiscourseEvent.track_events(:image_processing_finished) do
+ described_class.dominant_color(input_path:, timeout: 5)
+ end
+
+ expect(events.first[:params].first.except(:duration_seconds)).to eq(
+ operation: "upload_dominant_color",
+ success: true,
+ )
+ end
+end
diff --git a/spec/lib/letter_avatar_spec.rb b/spec/lib/letter_avatar_spec.rb
index 9098bfcc25d..8935f69b870 100644
--- a/spec/lib/letter_avatar_spec.rb
+++ b/spec/lib/letter_avatar_spec.rb
@@ -31,5 +31,22 @@ RSpec.describe LetterAvatar do
FileUtils.rm_f(described_class.fullsize_path(identity))
end
end
+
+ it "generates a PNG avatar with libvips when enabled" do
+ global_setting :enable_vips_image_processing, true
+ username = "A"
+ avatar_size = 45
+
+ generated_path = described_class.generate(username, avatar_size, cache: false)
+
+ expect(FastImage.type(generated_path)).to eq(:png)
+ expect(FastImage.size(generated_path)).to eq([avatar_size, avatar_size])
+ ensure
+ if generated_path
+ identity = LetterAvatar::Identity.from_username(username)
+ FileUtils.rm_f(generated_path)
+ FileUtils.rm_f(described_class.fullsize_path(identity))
+ end
+ end
end
end
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 20d772bfd55..6c7d9dc9697 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -936,6 +936,12 @@ RSpec.describe Upload do
upload.update!(url: Discourse.store.store_upload(file, upload))
upload
end
+ let(:ico_image) do
+ upload = Fabricate(:upload, extension: "ico")
+ file = file_from_fixtures("smallest.ico")
+ upload.update!(url: Discourse.store.store_upload(file, upload))
+ upload
+ end
let(:not_an_image) do
upload = Fabricate(:upload)
@@ -987,6 +993,36 @@ RSpec.describe Upload do
expect(cached_tiny_color).to eq(calculated_tiny_color)
end
+ it "uses libvips to calculate the dominant color when enabled" do
+ global_setting :enable_vips_image_processing, true
+
+ expect(tiny_image.dominant_color(calculate_if_missing: true)).to eq("565342")
+ end
+
+ it "normalizes a 16-bit dominant color when libvips is enabled" do
+ global_setting :enable_vips_image_processing, true
+
+ color = high_color_image.dominant_color(calculate_if_missing: true)
+
+ expect(color).to eq("00A0F0")
+ expect(high_color_image.dominant_color).to eq(color)
+ end
+
+ it "stores an empty dominant color for ICO images" do
+ global_setting :enable_vips_image_processing, true
+
+ expect(ico_image.dominant_color(calculate_if_missing: true)).to eq("")
+ expect(ico_image.dominant_color).to eq("")
+ end
+
+ it "stores an empty dominant color after image processing fails" do
+ global_setting :enable_vips_image_processing, true
+ DiscourseVips.stubs(:dominant_color).raises(DiscourseVips::Error).then.returns("FFFFFF")
+
+ expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
+ expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
+ end
+
it "can be backfilled" do
expect(white_image.dominant_color).to eq(nil)
expect(red_image.dominant_color).to eq(nil)
@@ -1033,18 +1069,24 @@ RSpec.describe Upload do
expect(invalid_image.dominant_color).to eq("")
end
- it "correctly handles unparsable ImageMagick output" do
+ it "raises when ImageMagick returns an invalid dominant color" do
ImageMagick.stubs(:magick).returns("someinvalidoutput")
expect(invalid_image.dominant_color).to eq(nil)
-
expect { invalid_image.dominant_color(calculate_if_missing: true) }.to raise_error(
/Calculated dominant color but unable to parse output/,
)
-
expect(invalid_image.dominant_color).to eq(nil)
end
+ it "stores an empty string for an invalid dominant color response" do
+ global_setting :enable_vips_image_processing, true
+ DiscourseVips.stubs(:dominant_color).returns("invalid")
+
+ expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
+ expect(white_image.dominant_color).to eq("")
+ end
+
it "correctly handles error when file is too large to download" do
white_image.stubs(:local?).returns(false)
FileHelper.stubs(:download).returns(nil)