From c8a31a32ea6e31c6630cb5e0ad876371d6c78840 Mon Sep 17 00:00:00 2001 From: Nicolas Raoul Date: Mon, 21 Jun 2021 13:05:46 +0900 Subject: [PATCH] Panorama (#4467) * panoramic images fixed * made requested changes * Minor refactoring Co-authored-by: Aditya Srivastava --- .../java/org/wikipedia/gallery/ImageInfo.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/data-client/src/main/java/org/wikipedia/gallery/ImageInfo.java b/data-client/src/main/java/org/wikipedia/gallery/ImageInfo.java index 4b8e63ad4..6ce92b0ff 100644 --- a/data-client/src/main/java/org/wikipedia/gallery/ImageInfo.java +++ b/data-client/src/main/java/org/wikipedia/gallery/ImageInfo.java @@ -29,6 +29,16 @@ public class ImageInfo implements Serializable { @Nullable private String user; @Nullable private String timestamp; + /** + * Query width, default width parameter of the API query in pixels. + */ + final private static int QUERY_WIDTH = 640; + + /** + * Threshold height, the minimum height of the image in pixels. + */ + final private static int THRESHOLD_HEIGHT = 220; + @NonNull public String getSource() { return StringUtils.defaultString(source); @@ -50,11 +60,24 @@ public class ImageInfo implements Serializable { return height; } + /** + * Get the thumbnail width. + * @return + */ + public int getThumbWidth() { return thumbWidth; } + + /** + * Get the thumbnail height. + * @return + */ + public int getThumbHeight() { return thumbHeight; } + @NonNull public String getMimeType() { return StringUtils.defaultString(mimeType, "*/*"); } @NonNull public String getThumbUrl() { + updateThumbUrl(); return StringUtils.defaultString(thumbUrl); } @@ -73,4 +96,26 @@ public class ImageInfo implements Serializable { @Nullable public ExtMetadata getMetadata() { return metadata; } + + /** + * Updates the ThumbUrl if image dimensions are not sufficient. + * Specifically, in panoramic images the height retrieved is less than required due to large width to height ratio, + * so we update the thumb url keeping a minimum height threshold. + */ + private void updateThumbUrl() { + // If thumbHeight retrieved from API is less than THRESHOLD_HEIGHT + if(getThumbHeight() < THRESHOLD_HEIGHT){ + // If thumbWidthRetrieved is same as queried width ( If not tells us that the image has no larger dimensions. ) + if(getThumbWidth() == QUERY_WIDTH){ + // Calculate new width depending on the aspect ratio. + final int finalWidth = (int)(THRESHOLD_HEIGHT * getThumbWidth() * 1.0 / getThumbHeight()); + thumbHeight = THRESHOLD_HEIGHT; + thumbWidth = finalWidth; + final String toReplace = "/" + QUERY_WIDTH + "px"; + final int position = thumbUrl.lastIndexOf(toReplace); + thumbUrl = (new StringBuilder(thumbUrl)).replace(position, position + toReplace.length(), "/" + thumbWidth + "px").toString(); + } + } + } + }