feat: new --bg specifying image background color for the preset svg and magick previewers (#3189)

This commit is contained in:
三咲雅 misaki masa 2025-09-21 22:18:00 +08:00 committed by GitHub
parent ba99ce4f8f
commit 8f9cce126c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View file

@ -31,13 +31,16 @@ function M:preload(job)
end
-- stylua: ignore
local status, err = cmd:arg {
cmd = cmd:arg {
tostring(job.file.url), "-auto-orient", "-strip",
"-sample", string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality", rt.preview.image_quality,
string.format("JPG:%s", cache),
}:status()
}
if job.args.bg then
cmd = cmd:arg { "-background", job.args.bg, "-alpha", "remove" }
end
local status, err = cmd:arg(string.format("JPG:%s", cache)):status()
if not status then
return true, Err("Failed to start `magick`, error: %s", err)
elseif not status.success then

View file

@ -29,19 +29,25 @@ function M:preload(job)
local cmd = Command("resvg"):arg {
"-w", rt.preview.max_width, "-h", rt.preview.max_height,
"--image-rendering", "optimizeSpeed",
tostring(job.file.url), tostring(cache)
}
if job.args.bg then
cmd = cmd:arg { "--background", job.args.bg }
end
if rt.tasks.image_alloc > 0 then
cmd = cmd:memory(rt.tasks.image_alloc)
end
local child, err = cmd:spawn()
local child, err = cmd:arg({ tostring(job.file.url), tostring(cache) }):spawn()
if not child then
return true, Err("Failed to start `resvg`, error: %s", err)
end
local status, err
while true do
if rt.tasks.image_alloc == 0 then
status, err = child:wait()
end
while not status and not err do
ya.sleep(0.2)
status, err = child:try_wait()
@ -56,7 +62,6 @@ function M:preload(job)
if mem and mem > rt.tasks.image_alloc then
child:start_kill()
err = Err("memory limit exceeded, pid: %s, memory: %s", id, mem)
break
end
end