1
0
Fork 1
mirror of https://akkoma.dev/AkkomaGang/akkoma.git synced 2024-11-19 03:19:17 +00:00

Support PNG previews to preserve alpha channels

This commit is contained in:
Mark Felder 2020-08-30 09:17:24 -05:00
parent 24d522c3b3
commit 2d2af75777
2 changed files with 47 additions and 2 deletions

View file

@ -23,6 +23,23 @@ def image_resize(url, options) do
end end
end end
defp prepare_image_resize_args(
%{max_width: max_width, max_height: max_height, format: "png"} = options
) do
quality = options[:quality] || 85
resize = Enum.join([max_width, "x", max_height, ">"])
args = [
"-resize",
resize,
"-quality",
to_string(quality),
"png:-"
]
{:ok, args}
end
defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
quality = options[:quality] || 85 quality = options[:quality] || 85
resize = Enum.join([max_width, "x", max_height, ">"]) resize = Enum.join([max_width, "x", max_height, ">"])

View file

@ -76,8 +76,12 @@ defp handle_preview("image/gif" = _content_type, conn, url) do
redirect(conn, external: mediaproxy_url) redirect(conn, external: mediaproxy_url)
end end
defp handle_preview("image/png" <> _ = _content_type, conn, url) do
handle_png_preview(conn, url)
end
defp handle_preview("image/" <> _ = _content_type, conn, url) do defp handle_preview("image/" <> _ = _content_type, conn, url) do
handle_image_preview(conn, url) handle_jpeg_preview(conn, url)
end end
defp handle_preview("video/" <> _ = _content_type, conn, url) do defp handle_preview("video/" <> _ = _content_type, conn, url) do
@ -88,7 +92,31 @@ defp handle_preview(content_type, conn, _url) do
send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.") send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
end end
defp handle_image_preview(%{params: params} = conn, url) do defp handle_png_preview(%{params: params} = conn, url) do
quality = Config.get!([:media_preview_proxy, :image_quality])
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
{:ok, thumbnail_binary} <-
MediaHelper.image_resize(
url,
%{
max_width: thumbnail_max_width,
max_height: thumbnail_max_height,
quality: quality,
format: "png"
}
) do
conn
|> put_resp_header("content-type", "image/png")
|> put_resp_header("content-disposition", "inline; filename=\"preview.png\"")
|> send_resp(200, thumbnail_binary)
else
_ ->
send_resp(conn, :failed_dependency, "Can't handle preview.")
end
end
defp handle_jpeg_preview(%{params: params} = conn, url) do
quality = Config.get!([:media_preview_proxy, :image_quality]) quality = Config.get!([:media_preview_proxy, :image_quality])
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params), with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),