Skip to content

Commit 70fbe99

Browse files
committed
new MediaUtils function: getImageWidthSettingFromString
1 parent 1ff8fe9 commit 70fbe99

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.TimeZone;
3030

3131
public class MediaUtils {
32+
private static final int DEFAULT_MAX_IMAGE_WIDTH = 1024;
33+
3234
public static boolean isValidImage(String url) {
3335
if (url == null) {
3436
return false;
@@ -114,29 +116,52 @@ public static Uri getLastRecordedVideoUri(Activity activity) {
114116
return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0));
115117
}
116118

117-
// Calculate the minimun width between the blog setting and picture real width
118-
public static int getMinimumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) {
119-
int imageWidthBlogSetting = Integer.MAX_VALUE;
120-
121-
if (!imageWidthBlogSettingString.equals("Original Size")) {
122-
try {
123-
imageWidthBlogSetting = Integer.valueOf(imageWidthBlogSettingString);
124-
} catch (NumberFormatException e) {
125-
AppLog.e(T.POSTS, e);
126-
}
119+
/**
120+
* Get image width setting from the image width site setting string. This string can be an int, in this case it's
121+
* the maximum image width defined by the site.
122+
* Examples:
123+
* "1000" will return 1000
124+
* "Original Size" will return Integer.MAX_VALUE
125+
* "Largeur originale" will return Integer.MAX_VALUE
126+
* null will return Integer.MAX_VALUE
127+
* @param imageWidthSiteSettingString Image width site setting string
128+
* @return Integer.MAX_VALUE if image width is not defined or invalid, maximum image width in other cases.
129+
*/
130+
public static int getImageWidthSettingFromString(String imageWidthSiteSettingString) {
131+
if (imageWidthSiteSettingString == null) {
132+
return Integer.MAX_VALUE;
133+
}
134+
try {
135+
return Integer.valueOf(imageWidthSiteSettingString);
136+
} catch (NumberFormatException e) {
137+
return Integer.MAX_VALUE;
127138
}
139+
}
128140

129-
int[] dimensions = ImageUtils.getImageSize(curStream, context);
130-
int imageWidthPictureSetting = dimensions[0] == 0 ? Integer.MAX_VALUE : dimensions[0];
141+
/**
142+
* Calculate and return the maximum allowed image width by comparing the width of the image at its full size with
143+
* the maximum upload width set in the blog settings
144+
* @param imageWidth the image's natural (full) width
145+
* @param imageWidthSiteSettingString the maximum upload width set in the site settings
146+
* @return maximum allowed image width
147+
*/
148+
public static int getMaximumImageWidth(int imageWidth, String imageWidthSiteSettingString) {
149+
int imageWidthBlogSetting = getImageWidthSettingFromString(imageWidthSiteSettingString);
150+
int imageWidthPictureSetting = imageWidth == 0 ? Integer.MAX_VALUE : imageWidth;
131151

132152
if (Math.min(imageWidthPictureSetting, imageWidthBlogSetting) == Integer.MAX_VALUE) {
133-
// Default value in case of errors reading the picture size and the blog settings is set to Original size
134-
return 1024;
153+
// Default value in case of errors reading the picture size or the blog settings is set to Original size
154+
return DEFAULT_MAX_IMAGE_WIDTH;
135155
} else {
136156
return Math.min(imageWidthPictureSetting, imageWidthBlogSetting);
137157
}
138158
}
139159

160+
public static int getMaximumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) {
161+
int[] dimensions = ImageUtils.getImageSize(curStream, context);
162+
return getMaximumImageWidth(dimensions[0], imageWidthBlogSettingString);
163+
}
164+
140165
public static boolean isInMediaStore(Uri mediaUri) {
141166
// Check if the image is externally hosted (Picasa/Google Photos for example)
142167
if (mediaUri != null && mediaUri.toString().startsWith("content://media/")) {

0 commit comments

Comments
 (0)