Skip to content

Commit c8a31a3

Browse files
Panorama (#4467)
* panoramic images fixed * made requested changes * Minor refactoring Co-authored-by: Aditya Srivastava <iamaditya2009@gmail.com>
1 parent 810c797 commit c8a31a3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

data-client/src/main/java/org/wikipedia/gallery/ImageInfo.java

+45
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public class ImageInfo implements Serializable {
2929
@Nullable private String user;
3030
@Nullable private String timestamp;
3131

32+
/**
33+
* Query width, default width parameter of the API query in pixels.
34+
*/
35+
final private static int QUERY_WIDTH = 640;
36+
37+
/**
38+
* Threshold height, the minimum height of the image in pixels.
39+
*/
40+
final private static int THRESHOLD_HEIGHT = 220;
41+
3242
@NonNull
3343
public String getSource() {
3444
return StringUtils.defaultString(source);
@@ -50,11 +60,24 @@ public int getHeight() {
5060
return height;
5161
}
5262

63+
/**
64+
* Get the thumbnail width.
65+
* @return
66+
*/
67+
public int getThumbWidth() { return thumbWidth; }
68+
69+
/**
70+
* Get the thumbnail height.
71+
* @return
72+
*/
73+
public int getThumbHeight() { return thumbHeight; }
74+
5375
@NonNull public String getMimeType() {
5476
return StringUtils.defaultString(mimeType, "*/*");
5577
}
5678

5779
@NonNull public String getThumbUrl() {
80+
updateThumbUrl();
5881
return StringUtils.defaultString(thumbUrl);
5982
}
6083

@@ -73,4 +96,26 @@ public int getHeight() {
7396
@Nullable public ExtMetadata getMetadata() {
7497
return metadata;
7598
}
99+
100+
/**
101+
* Updates the ThumbUrl if image dimensions are not sufficient.
102+
* Specifically, in panoramic images the height retrieved is less than required due to large width to height ratio,
103+
* so we update the thumb url keeping a minimum height threshold.
104+
*/
105+
private void updateThumbUrl() {
106+
// If thumbHeight retrieved from API is less than THRESHOLD_HEIGHT
107+
if(getThumbHeight() < THRESHOLD_HEIGHT){
108+
// If thumbWidthRetrieved is same as queried width ( If not tells us that the image has no larger dimensions. )
109+
if(getThumbWidth() == QUERY_WIDTH){
110+
// Calculate new width depending on the aspect ratio.
111+
final int finalWidth = (int)(THRESHOLD_HEIGHT * getThumbWidth() * 1.0 / getThumbHeight());
112+
thumbHeight = THRESHOLD_HEIGHT;
113+
thumbWidth = finalWidth;
114+
final String toReplace = "/" + QUERY_WIDTH + "px";
115+
final int position = thumbUrl.lastIndexOf(toReplace);
116+
thumbUrl = (new StringBuilder(thumbUrl)).replace(position, position + toReplace.length(), "/" + thumbWidth + "px").toString();
117+
}
118+
}
119+
}
120+
76121
}

0 commit comments

Comments
 (0)