Try specifying fd0, force jpg out
[akkoma] / lib / pleroma / helpers / media_helper.ex
index ecd234558fb04f3a472de3b45bab13668efb7119..01f42d9b03f164ae2541b0637b687d0417d3822d 100644 (file)
@@ -7,19 +7,34 @@ defmodule Pleroma.Helpers.MediaHelper do
   Handles common media-related operations.
   """
 
-  def ffmpeg_resize_remote(uri, %{max_width: max_width, max_height: max_height}) do
+  def image_resize(url, %{max_width: max_width, max_height: max_height} = options) do
+    quality = options[:quality] || 85
+
     cmd = ~s"""
-    curl -L "#{uri}" |
-    ffmpeg -i pipe:0 -f lavfi -i color=c=white \
-      -filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
-        force_original_aspect_ratio=decrease [scaled]; \
-        [1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
-      -f image2 -vcodec mjpeg -frames:v 1 pipe:1 | \
-    cat
+    convert fd:0 -resize '#{max_width}x#{max_height}>' -quality #{quality} jpg:-
     """
 
-    with {:ok, [stdout: stdout_list]} <- Pleroma.Exec.cmd(cmd) do
-      {:ok, Enum.join(stdout_list)}
+    pid = Port.open({:spawn, cmd}, [:use_stdio, :stream, :exit_status, :binary])
+    {:ok, env} = url |> Pleroma.Web.MediaProxy.url() |> Pleroma.HTTP.get()
+    image = env.body
+    Port.command(pid, image)
+    loop_recv(pid)
+  end
+
+  defp loop_recv(pid) do
+    loop_recv(pid, <<>>)
+  end
+
+  defp loop_recv(pid, acc) do
+    receive do
+      {^pid, {:data, data}} ->
+        loop_recv(pid, acc <> data)
+
+      {^pid, {:exit_status, 0}} ->
+        {:ok, acc}
+
+      {^pid, {:exit_status, status}} ->
+        {:error, status}
     end
   end
 end