From 70845c5e956ec19f072abdaca1ff308cc5c7a3f3 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 22 Sep 2018 16:51:00 +0200 Subject: [PATCH] do not compress/resize images with strange aspect ratios. fixes #3188 --- .../siacs/conversations/persistance/FileBackend.java | 12 ++++++++++++ .../services/XmppConnectionService.java | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 3382aa164..cad3789a8 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -687,6 +687,18 @@ public class FileBackend { updateFileParams(message); } + public boolean unusualBounds(Uri image) { + try { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(image), null, options); + float ratio = (float) options.outHeight / options.outWidth; + return ratio > (21.0f / 9.0f) || ratio < (9.0f / 21.0f); + } catch (Exception e) { + return false; + } + } + private int getRotation(File file) { return getRotation(Uri.parse("file://" + file.getAbsolutePath())); } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index ddc9bd6b5..8d63dc17a 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -504,7 +504,8 @@ public class XmppConnectionService extends Service { if ("never".equals(compressPictures) || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri)) - || (mimeType != null && mimeType.endsWith("/gif"))) { + || (mimeType != null && mimeType.endsWith("/gif")) + || getFileBackend().unusualBounds(uri)) { Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": not compressing picture. sending as file"); attachFileToConversation(conversation, uri, mimeType, callback); return;