Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CARE-5657] Fix - Crop to aspect ratio before passing to the renderer #1187

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/StoryCards/StoryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function StoryCard({
title={titleAsString}
>
<StoryImage
aspectRatio={4 / 3}
e1himself marked this conversation as resolved.
Show resolved Hide resolved
className={styles.image}
isStatic={withStaticImage}
placeholderClassName={styles.placeholder}
Expand Down
32 changes: 28 additions & 4 deletions components/StoryImage/StoryImage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import UploadcareImage from '@uploadcare/nextjs-loader';
import type { UploadcareImage } from '@prezly/uploadcare';
import UploadcareImageLoader from '@uploadcare/nextjs-loader';
import classNames from 'classnames';

import type { ListStory } from 'types';
Expand All @@ -12,6 +13,7 @@ import { getCardImageSizes, getStoryThumbnail, type ImageSize } from './lib';
import styles from './StoryImage.module.scss';

type Props = {
aspectRatio?: number;
className?: string;
isStatic?: boolean;
placeholderClassName?: string;
Expand All @@ -21,6 +23,7 @@ type Props = {
};

export function StoryImage({
aspectRatio,
className,
isStatic = false,
placeholderClassName,
Expand All @@ -30,12 +33,12 @@ export function StoryImage({
}: Props) {
const fallback = useFallback();
const image = getStoryThumbnail(thumbnailImage);
const uploadcareImage = getUploadcareImage(image);
const uploadcareImage = applyAspectRatio(getUploadcareImage(image), aspectRatio);

if (uploadcareImage) {
return (
<div className={classNames(styles.imageContainer, className)}>
<UploadcareImage
<UploadcareImageLoader
fill
alt={title}
className={classNames(styles.image, {
Expand All @@ -57,7 +60,7 @@ export function StoryImage({
})}
>
{fallbackImage ? (
<UploadcareImage
<UploadcareImageLoader
alt="No image"
src={fallbackImage.cdnUrl}
className={classNames(styles.imageContainer, styles.placeholderLogo, className)}
Expand All @@ -70,3 +73,24 @@ export function StoryImage({
</span>
);
}

function applyAspectRatio(
image: UploadcareImage | null,
aspectRatio: number | undefined,
): UploadcareImage | null {
if (!image || !aspectRatio) {
return image;
}

const actualAspectRatio = image.width / image.height;

if (actualAspectRatio > aspectRatio * 2) {
return image.scaleCrop(image.height * aspectRatio * 2, image.height, true);
}

if (actualAspectRatio < aspectRatio) {
return image.scaleCrop(image.width, image.width / aspectRatio, true);
}

return image;
}
Loading