Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8860916

Browse files
authored
Fix size of portrait images with the SIZE_NORMAL setting. (#7188)
1 parent 9db0ebb commit 8860916

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/components/views/messages/MImageBody.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,16 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
378378
// The maximum size of the thumbnail as it is rendered as an <img>
379379
// check for any height constraints
380380
const imageSize = SettingsStore.getValue("Images.size") as ImageSize;
381-
const suggestedAndPossibleWidth = Math.min(suggestedImageSize(imageSize).w, infoWidth);
382-
const suggestedAndPossibleHeight = Math.min(suggestedImageSize(imageSize).h, infoHeight);
381+
const isPortrait = infoWidth < infoHeight;
382+
const suggestedAndPossibleWidth = Math.min(suggestedImageSize(imageSize, isPortrait).w, infoWidth);
383+
const suggestedAndPossibleHeight = Math.min(suggestedImageSize(imageSize, isPortrait).h, infoHeight);
383384
const aspectRatio = infoWidth / infoHeight;
384385

385386
let maxWidth;
386387
let maxHeight;
387388
const maxHeightConstraint = forcedHeight || this.props.maxImageHeight || suggestedAndPossibleHeight;
388389
if (maxHeightConstraint * aspectRatio < suggestedAndPossibleWidth || imageSize === ImageSize.Large) {
389-
// width is dictated by the maximum height that was defined by the props or the function param `forcedHeight`
390+
// The width is dictated by the maximum height that was defined by the props or the function param `forcedHeight`
390391
// If the thumbnail size is set to Large, we always let the size be dictated by the height.
391392
maxWidth = maxHeightConstraint * aspectRatio;
392393
// there is no need to check for infoHeight here since this is done with `maxHeightConstraint * aspectRatio < suggestedAndPossibleWidth`

src/components/views/messages/MVideoBody.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
5959
};
6060
}
6161

62-
private get suggestedDimensions(): { w: number, h: number } {
62+
private suggestedDimensions(isPortrait): { w: number, h: number } {
6363
return suggestedVideoSize(SettingsStore.getValue("Images.size") as ImageSize);
6464
}
6565

@@ -69,23 +69,25 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
6969
thumbWidth?: number,
7070
thumbHeight?: number,
7171
): number {
72-
if (!thumbWidth || !thumbHeight) {
73-
const dims = this.suggestedDimensions;
74-
thumbWidth = dims.w;
75-
thumbHeight = dims.h;
76-
}
77-
7872
if (!fullWidth || !fullHeight) {
7973
// Cannot calculate thumbnail height for image: missing w/h in metadata. We can't even
8074
// log this because it's spammy
8175
return undefined;
8276
}
77+
78+
if (!thumbWidth || !thumbHeight) {
79+
const dims = this.suggestedDimensions(fullWidth < fullHeight);
80+
thumbWidth = dims.w;
81+
thumbHeight = dims.h;
82+
}
83+
8384
if (fullWidth < thumbWidth && fullHeight < thumbHeight) {
8485
// no scaling needs to be applied
8586
return 1;
8687
}
87-
const widthMulti = thumbWidth / fullWidth;
88+
8889
// always scale the videos based on their width.
90+
const widthMulti = thumbWidth / fullWidth;
8991
return widthMulti;
9092
}
9193

@@ -268,7 +270,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
268270

269271
const contentUrl = this.getContentUrl();
270272
const thumbUrl = this.getThumbUrl();
271-
const defaultDims = this.suggestedDimensions;
273+
const defaultDims = this.suggestedDimensions(false);
272274
let height = defaultDims.h;
273275
let width = defaultDims.w;
274276
let poster = null;

src/settings/enums/ImageSize.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// For Large the image gets drawn as big as possible.
18+
// constraint by: timeline width, manual heigh overrides, SIZE_LARGE.h
1719
const SIZE_LARGE = { w: 800, h: 600 };
18-
const SIZE_NORMAL = { w: 324, h: 220 };
1920

21+
// For Normal the image gets drawn to never exceed SIZE_NORMAL.w, SIZE_NORMAL.h
22+
// constraint by: timeline width, manual heigh overrides
23+
const SIZE_NORMAL_LANDSCAPE = { w: 324, h: 324 }; // for w > h
24+
const SIZE_NORMAL_PORTRAIT = { w: 324 * (9/16), h: 324 }; // for h > w
2025
export enum ImageSize {
2126
Normal = "normal",
2227
Large = "large",
2328
}
2429

25-
export function suggestedSize(size: ImageSize): { w: number, h: number } {
30+
export function suggestedSize(size: ImageSize, portrait = false): { w: number, h: number} {
2631
switch (size) {
2732
case ImageSize.Large:
2833
return SIZE_LARGE;
2934
case ImageSize.Normal:
3035
default:
31-
return SIZE_NORMAL;
36+
return portrait ? SIZE_NORMAL_PORTRAIT : SIZE_NORMAL_LANDSCAPE;
3237
}
3338
}

0 commit comments

Comments
 (0)