From d82bb4f33f0a44b8374332f1f8a3f03dc7c639ff Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 29 Apr 2020 23:29:05 -0400 Subject: [PATCH] fixes #74: only reencode the image when there is no choice to fix the orientation --- lib/philomena/processors/jpeg.ex | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/philomena/processors/jpeg.ex b/lib/philomena/processors/jpeg.ex index 59535d8e..8cbff441 100644 --- a/lib/philomena/processors/jpeg.ex +++ b/lib/philomena/processors/jpeg.ex @@ -26,7 +26,17 @@ defmodule Philomena.Processors.Jpeg do defp strip(file) do stripped = Briefly.create!(extname: ".jpg") - {_output, 0} = System.cmd("convert", [file, "-auto-orient", "-strip", stripped]) + # ImageMagick always reencodes the image, resulting in quality loss, so + # be more clever + case System.cmd("identify", ["-format", "%[orientation]", file]) do + {"Undefined", 0} -> + # Strip EXIF without touching orientation + {_output, 0} = System.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file]) + + {_output, 0} -> + # Strip EXIF and reorient image + {_output, 0} = System.cmd("convert", [file, "-auto-orient", "-strip", stripped]) + end stripped end